Why this javac: File Not Found error?

喜欢而已 提交于 2019-12-17 14:14:12

问题


Working with some basic java apps on CentOS 5 linux and I have my classpath set to point to home/pathToJava/bin which contains javac and java

and I have .java files in home/pathToFolderA/src

and home/pathToFolderB/gen-java

When I run javac and java in home/pathToFolderA/src everything works perfectly

But when I run javac from within home/pathToFolderB/gen-java on fileName.java I get a file not found error, specifically

javac: file Not found: fileName.java
Usage: javac <options> <source files>

Why could this be happening?

Thanks for all help


回答1:


You shouldn't set your classpath to point to your JDK bin directory -- instead it should be the PATH environment variable, which serves a different purpose to classpath. (The classpath defines a list of jars and directories containing compiled Java .class code; the PATH variable defines a list of paths where the shell needs to look and locate programs to execute when they are not found in the current directory -- so if you type for instance zip -- it would look in all the directories defined in PATH and figure out that zip program is located under /usr/bin) Secondly if you want to compile sources from both directory you need to specify:

  • all the paths where the sources are (both home/pathToFolderA/src and home/pathToFolderB/gen-java)
  • the path where the compiled .class files to be generated
  • specify in the classpath any library you might use in your source files

To sum it up, it would be something like this to compile:

javac -d /home/pathToFolderWithResultsOfCompilation -classpath /path/to/some.jar:/path/to/another.jar home/pathToFolderA/src/*.java home/pathToFolderB/gen-java/*.java 

and to run your compiled programs:

java -classpath /path/to/some.jar:/path/to/another.jar:/home/pathToFolderWithResultsOfCompilation full.name.of.your.Java



回答2:


The classpath is used to find class files, not source files. (Nor is it used to find the java and javac binaries; those are found in your normal path.) You need to specify the files to compile explicitly:

javac /home/pathToFolderA/src/fileName.java

Obviously if you're already in /home/pathToFolderA/src then you can just use fileName.java because that's treated as being relative to your current directory.




回答3:


Working with some basic java apps on CentOS 5 linux and I have my classpath set to point to home/pathToJava/bin which contains javac and java

That's wrong. The classpath is used to find *.class files, not operating system specific executables. The bin directory of your JDK does not belong in the classpath. Note that the classpath is also not for finding *.java source files.

When you run javac you need to specify the path to the source file, if it isn't in the current directory.




回答4:


make sure that your file name contain no spaces

Eg:

HelloWorld.java

usually the errors occur when you rename the file by copy past that will cause a space between the name and the dot (this is the mistake:HelloWorld .java).

and make sure you changed the directory to the same folder your file in




回答5:


Without a listing of the directory "gen-java" and the exact command you're typing,my guess would be that you're trying to compile a file that doesn't exist. Linux is case sensitive, so maybe that's your problem. Or the file doesn't exist.



来源:https://stackoverflow.com/questions/6108598/why-this-javac-file-not-found-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!