0

I know the title doesn't fully describe my problem, but I'm not sure how else to ask this. I know there's tools to do this, but I want to understand how the compiler works in java, I wrote some very basic code and I'm able to compile them without any problems using

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

in the command line, but that only works when the packages are in the same source directory as seen in the picture below

Packages are in the same source directory

I want to be able to compile from the command line where my package are in other source directories as well, please see the picture below

Packages are in multiple sources

Does anyone know how I can do this ? I tried modifying the command line to

javac -sourcepath java -d ../target/ java/dir1/shapes/mainclass/TestRun.java but this results in the following error

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

Any help will be appreciated

1
  • An alternative would be to define two different projects. Commented Jan 15, 2018 at 13:59

3 Answers 3

1

I managed to find a solution, you should use javac -sourcepath java/dir1;java/dir2 -d ../target/dir1 java/dir1/shapes/mainclass/TestRun.java

notice that on the -sourcepath its not -sourcepath java but its -sourcepath java/dir1;java/dir2, its specifying where the .java classes are in each directory

Sign up to request clarification or add additional context in comments.

Comments

0

Use -d command line parameter with javac to tell it what directory you'd like to store the compiled class files in. After complication run the program, using java -cp

javac -d some/directory javaprogram.java

java - cp some/directory javaprogram

Comments

0

You should be in thesrc/java directory, and adjust your commands accordingly.

2 Comments

I was able to achieve my goal from the src directory
No doubt, but this is the easy way to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.