Java编译器的classpath / cp选项无法找到源文件。

huangapple go评论70阅读模式
英文:

Javac classpath / cp option not able to find the source file

问题

我有一个位于以下位置的源文件Example.java:
`C:\Users\sushr\Desktop\Experimental Java code\tutorial`
从教程目录运行dir命令的结果:

    C:\Users\sushr\Desktop\Experimental Java code\tutorial的目录
    10/10/2020  01:51 PM    <DIR>          .
    10/10/2020  01:51 PM    <DIR>          ..
    10/10/2020  01:51 PM               133 Example.java        <- 这是源文件

我正在尝试从C:\编译此文件。
我从命令提示符运行的命令如下:

`C:\>javac -cp "C:\Users\sushr\Desktop\Experimental Java code\tutorial" Example.java`

我收到以下错误消息:

    error: file not found: Example.java
    Usage: javac <options> <source files>
    use --help for a list of possible options
英文:

I have a source file Example.java in the following location:
C:\Users\sushr\Desktop\Experimental Java code\tutorial
Result of dir command from tutorial directory:

Directory of C:\Users\sushr\Desktop\Experimental Java code\tutorial
10/10/2020  01:51 PM    &lt;DIR&gt;          .
10/10/2020  01:51 PM    &lt;DIR&gt;          ..
10/10/2020  01:51 PM               133 Example.java        &lt;- This is the source file 

I am trying to compile this file from location C:\ .
The command that I am running from the command prompt is the following:

C:\&gt;javac -cp &quot;C:\Users\sushr\Desktop\Experimental Java code\tutorial&quot; Example.java

I am getting the following error:

error: file not found: Example.java
Usage: javac &lt;options&gt; &lt;source files&gt;
use --help for a list of possible options

答案1

得分: 2

如果您指定了您的.java文件的绝对路径,您应该能够直接编译它,而无需使用-cp标志,就像这样:

C:&gt;javac &quot;C:\Users\sushr\Desktop\Experimental Java code\tutorial\Example.java&quot;
英文:

If you specify the absolute path to your .java file you should be able to just compile it without the -cp flag like so:

C:&gt;javac &quot;C:\Users\sushr\Desktop\Experimental Java code\tutorial\Example.java&quot;

答案2

得分: 2

The classpath setting for javac is for finding other libraries and classes while compiling your .java files. It is not used for finding the .java files you specified as an argument for the javac program. When you call javac Example.java and you are currently in the directory C:\, then it will look for a file C:\Example.java. It is most likely that the Example.java file will not be directly in the file system root C:\.

Either specify the .java files with an absolute path or adjust your working directory with cd "C:\Users\sushr\Desktop\Experimental Java code\tutorial\" to go to that directory and compile the files from that location.

英文:

The classpath setting for javac is for finding other libraries and classes while compiling your .java files. It is not used for finding the .java files you specified as argument for the javac program. When you call javac Example.java and you are currently in the directory C:\, then it will look for a file C:\Example.java. It is most likely that the Example.java file will not be directly in the file system root C:\.

Either specify the .java files with an absolute path or adjust your working directory with cd &quot;C:\Users\sushr\Desktop\Experimental Java code\tutorial\&quot; to go in that directory and compile the files from that location.

答案3

得分: 0

在macOS中,我注意到使用"~/"作为前往主目录的快捷方式是无效的。例如:

javac -cp .:~/algs4/algs4.jar MyFile.java

相反地,我不得不使用完整路径来定位.jar文件以进行编译:

javac -cp .:/Users/username/algs4/algs4.jar MyFile.java
英文:

In macOS, I have noticed that using the "~/" to shortcut to home, it does not work. For example:

javac -cp .:~/algs4/algs4.jar MyFile.java

Instead, I had to use the full path to locate the .jar file in order to compile:

javac -cp .:/Users/username/algs4/algs4.jar MyFile.java

huangapple
  • 本文由 发表于 2020年10月11日 05:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64298335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定