简体   繁体   中英

I can access javac and java from command, but not .class files and .java files in the same directory

I'm trying to use cmd to compile a .class file using javac. I have set the PATH variable, and I can reach javac.exe and java.exe by opening cmd from the start menu.

I have a .class file in the same folder with javac.exe and java.exe. Yet, for some reason, the file cannot be found. While I can manually set the directory each time using cd, it's a bit tedious to type out the path each time I want to compile a .class file.

Am I required to set the directory each time? Or am I missing something?

The OS looks for commands or more specifically executable files like in the windows case .BAT files, .EXE files, .COM or .CMD. When you execute Java.exe windows looks first at local diretory then searches the PATH in each diretory for the executable filename "java.exe". After that is the Java.exe that looks for the variable you entered after like the file.class or file.java, and it doesn't access the windows path variable for that.

So you have to either be on the folder you have the .class/.java file or write the path for that file.

If you want to compile .java that has .class dependency, run javac as

javac -cp <path to .class root folder> <path to .java>

run javac without parameters to see a short help, or go to http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/javac.html for full description

必须将类路径设置为当前目录

javac -cp path/to path/to/JavaSourceFile.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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