[英]Unable to compile .java file using javac at Windows command prompt
[英]Java file compile using javac unable to refer to other files in the same directory
我正在尝试从像 Eclipse 这样的 IDE 迁移到独立的 Java 环境,但是我在将多个文件绑定到一个项目中时遇到了问题。
这是一些示例代码,其中两个文件位于同一目录中:
App.java
package com.example.main;
public class App {
public static void main(String[] args) {
Example.test();
}
}
例.java
package com.example.main;
public class Example {
public static void test() {
System.out.println("It's working");
}
}
When running App.java
in an IDE, the expected output of It's working
is printed, however after executing javac *.java
the files seem to ignore eachother.
这是编译后执行java App.java
时出现的错误:
App.java:5: error: cannot find symbol
Example.test();
^
symbol: variable Example
location: class App
1 error
error: compilation failed
如何编译项目中的文件以便它们相互识别?
如果您运行App.java
11及以上, java App.java
如果您需要参考Example.java
,首先您需要将两个 java 文件编译到一个目录中。 让它命名为“类”。 该命令将是
javac -d classes *.java
之后,您可以通过
java -cp classes com.example.main.App
。 请注意,App 没有.class
后缀
当然,如果项目变得更大或需要其他依赖项,建议使用Apache Maven或Gradle 之类的构建工具来构建您的项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.