Java命令使用多个源

时间:2018-01-15 13:49:05

标签: java command-line compilation

我知道标题并没有完全描述我的问题,但我不知道怎么回答这个问题。我知道有工具可以做到这一点,但我想了解编译器如何在java中工作,我写了一些非常基本的代码,我能够编译它们而没有任何问题使用

javac -sourcepath java -d ../target/dir1 java/shapes/mainclass/TestRun.java

在命令行中,但仅当packages位于同一源目录中时才有效,如下图所示

Packages are in the same source directory

我希望能够从我的package在其他源目录中的命令行进行编译,请参阅下面的图片

Packages are in multiple sources

有谁知道我怎么做到这一点?我尝试将命令行修改为

javac -sourcepath java -d ../target/ java/dir1/shapes/mainclass/TestRun.java但这会导致以下错误

java\dir1\shapes\mainclass\TestRun.java:3: error: package shapes.classes does not exist
import shapes.classes.*;
^
java\dir1\shapes\mainclass\TestRun.java:8: error: cannot find symbol
                ShapeMain circle = new Circle(5);
                ^
  symbol:   class ShapeMain
  location: class TestRun
java\dir1\shapes\mainclass\TestRun.java:8: error: cannot find symbol
                ShapeMain circle = new Circle(5);
                                       ^
  symbol:   class Circle
  location: class TestRun
java\dir1\shapes\mainclass\TestRun.java:9: error: cannot find symbol
                ShapeMain rectangle = new Rectangle(8, 9.9);
                ^
  symbol:   class ShapeMain
  location: class TestRun
java\dir1\shapes\mainclass\TestRun.java:9: error: cannot find symbol
                ShapeMain rectangle = new Rectangle(8, 9.9);
                                          ^
  symbol:   class Rectangle
  location: class TestRun
java\dir1\shapes\mainclass\TestRun.java:10: error: cannot find symbol
                ShapeMain triangle = new Triangle(10, 4.5);
                ^
  symbol:   class ShapeMain
  location: class TestRun
java\dir1\shapes\mainclass\TestRun.java:10: error: cannot find symbol
                ShapeMain triangle = new Triangle(10, 4.5);
                                         ^
  symbol:   class Triangle
  location: class TestRun
7 errors

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:1)

我设法找到了解决方案,您应该使用javac -sourcepath java/dir1;java/dir2 -d ../target/dir1 java/dir1/shapes/mainclass/TestRun.java

请注意,-sourcepath上的-sourcepath java不是-sourcepath java/dir1;java/dir2,而是.java,它指定每个目录中^类的位置

答案 1 :(得分:0)

使用-d命令行参数和javac告诉它你要在哪个目录中存储已编译的类文件。复制后运行程序,使用java -cp

javac -d some/directory javaprogram.java

java - cp some / directory javaprogram

答案 2 :(得分:0)

您应该 src/java目录中,并相应地调整命令。