简体   繁体   English

Javac 类路径/cp 选项找不到源文件

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

I have a source file Example.java in the following location:我在以下位置有一个源文件 Example.java:
C:\\Users\\sushr\\Desktop\\Experimental Java code\\tutorial
Result of dir command from tutorial directory:教程目录中 dir 命令的结果:

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:\\ .我正在尝试从位置 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

The classpath setting for javac is for finding other libraries and classes while compiling your .java files. javac的类路径设置用于在编译.java文件时查找其他库和类。 It is not used for finding the .java files you specified as argument for the javac program.它不用于查找您指定为javac程序参数的.java文件。 When you call javac Example.java and you are currently in the directory C:\\ , then it will look for a file C:\\Example.java .当您调用javac Example.java并且您当前位于目录C:\\ ,它将查找文件C:\\Example.java It is most likely that the Example.java file will not be directly in the file system root C:\\ . Example.java文件很可能不会直接位于文件系统根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.使用绝对路径指定.java文件或使用cd "C:\\Users\\sushr\\Desktop\\Experimental Java code\\tutorial\\"调整您的工作目录以进入该目录并从该位置编译文件。

如果您指定 .java 文件的绝对路径,您应该能够在没有 -cp 标志的情况下编译它,如下所示:

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

In macOS, I have noticed that using the "~\/" to shortcut to home, it does not work.在 macOS 中,我注意到使用“~\/”来快捷方式回家,它不起作用。 For example:例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM