I am trying to compile (from the command line) a java package that imports another package of my own. I was following a tutorial online but it seems that I get an error when I try to compile the final java file (CallPackage.java).
Here is the file structure:
+ test_directory (contains CallPackage.java) -> importpackage -> subpackage (contains HelloWorld.java)
Here is CallPackage.java:
/// CallPackage.java import importpackage.subpackage.*; class CallPackage{ public static void main(String[] args){ HelloWorld h2=new HelloWorld(); h2.show(); } }
and here is HelloWorld.java:
///HelloWorld.java package importpackage.subpackage; public class HelloWorld { public void show(){ System.out.println("This is the function of the class HelloWorld!!"); } }
$javac HelloWorld.java
.$javac CallPackage.java
.This gives me an error on the last command:
CallPackage.java:1: package importpackage.subpackage does not exist import importpackage.subpackage.*; ^ CallPackage.java:4: cannot find symbol symbol : class HelloWorld location: class CallPackage HelloWorld h2=new HelloWorld(); ^ CallPackage.java:4: cannot find symbol symbol : class HelloWorld location: class CallPackage HelloWorld h2=new HelloWorld(); ^ 3 errors
How can I compile both packages? Thanks so much for any help!
The issue was that the class path needs to be set for each command (javac and java):
Attempted Steps
instead of going to subpackage, compile HelloWorld.java from the top_level:
$javac -cp . importpackage/subpackage/HelloWorld.java
compile CallPackage.java in the same way:
$javac -cp . CallPackage.java
run the file using the class path also:
$java -cp . CallPackage
NOTE: running "$java CallPackage" will give an error "Error: Could not find or load main class CallPackage"
In summary, during each step, the class path must be specified. It worked after running it as such.
Same situation to me. And I came to take over it by compiling classes at the same time.
For example, here is my project:
+ beerV1 -> classes -> src -> com -> example -> model -> BeerExpert.java -> web -> BeerSelect.java
BeerExpert.java:
package com.example.model; import ... public class BeerExpert{ ... }
BeerSelect.java:
package com.example.web; import com.example.model.*; import ... public class BeerSelect { ... }
As you can see: BeerSelect.java is trying to import classes in com.example.model package.
At the first time, I compiled BeerExert.java first by command: --> javac -d classes src/com/example/model/BeerExpert.java
Then: --> javac -d classes src/com/example/web/BeerSelect.java
And the result was: -->... error: package com.example.model does not exist
So, I knew that compiling multiple classes separately will not work in this case.
After suffering on google, I found this very simple way to solve the problem:
Just compile all at once:
--> javac -d classes src/com/example/model/BeerExpert.java src/com/example/web/BeerSelect.java
Finally, here is what I got:
+ beerV1 -> classes -> com -> example -> model -> BeerExpert.class -> web -> BeerSelect.class -> src -> com -> example -> model -> BeerExpert.java -> web -> BeerSelect.java
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With