英文:
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 <DIR> .
10/10/2020 01:51 PM <DIR> ..
10/10/2020 01:51 PM 133 Example.java <- 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:\>javac -cp "C:\Users\sushr\Desktop\Experimental Java code\tutorial" Example.java
I am getting the following error:
error: file not found: Example.java
Usage: javac <options> <source files>
use --help for a list of possible options
答案1
得分: 2
如果您指定了您的.java
文件的绝对路径,您应该能够直接编译它,而无需使用-cp
标志,就像这样:
C:>javac "C:\Users\sushr\Desktop\Experimental Java code\tutorial\Example.java"
英文:
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:>javac "C:\Users\sushr\Desktop\Experimental Java code\tutorial\Example.java"
答案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 "C:\Users\sushr\Desktop\Experimental Java code\tutorial\"
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论