简体   繁体   English

javac(compile)和java(run)包在不同的目录结构级别上工作?

[英]javac(compile) and java(run) package work at different directory structure levels?

I just noticed that for compiling I need to specify the exact path from my current location for the java source files that needs to be compiled, but to run the class file, all I need is the class qualified name. 我只是注意到,对于编译,我需要为需要编译的Java源文件从当前位置指定确切的路径,但是要运行类文件,我需要的只是类限定名。

Does the java command recursively look inside all the folders in my current directory to find the class and execute it? java命令是否以递归方式查找当前目录中的所有文件夹以查找该类并执行它?

if so why doesn't the javac work similarly but expects to have the absolute path? 如果是这样,为什么javac不能类似地工作,但是期望具有绝对路径?

From my project folder location, 在我的项目文件夹位置,

javac -cp ".:/Users/page/.m2/repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar:target/classes" src/main/java/app/Assignment04.java


java -cp ".:/Users/page/.m2/repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar:target/classes" app.Assignment04

EDIT: when I explicitly mentioned path like this; 编辑:当我明确提到这样的路径时; java -cp ".:/Users/page/.m2/repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar:src/main/java/com/scg/util/*:src/main/java/com/scg/domain/*:src/main/java/app/*" app.Assignment04

I get Error: Could not find or load main class app.Assignment04 我收到Error: Could not find or load main class app.Assignment04

although I give src/main/java/app/ in my classpath 虽然我在classpath给出了src/main/java/app/

-cp ".:/Users/page/.m2/repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar:target/classes"

is the important bit. 是重要的一点。 When compiling the classpath argument is not considered for the source files themselves (only to resolve dependencies). 编译时,源文件本身不考虑classpath参数(仅用于解决依赖关系)。

When running with java the named class is sought on the classpath, which includes the target directory explicitly as target/classes . 使用java运行时,将在类路径上搜索命名的类,该类路径将目标目录显式包含target/classes Thus that location is also used to find the class in question. 因此,该位置也可用于查找相关类别。

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

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