Skip to content
Advertisement

Can’t access file when Compile 2 package

I’m a newbie, I have learned Java for 2 months by self-study. I often use the command line to run files because I use Atom IDE. I just want to complile 2 simple packages “package1” & “package2”. Like the image, these 2 packages and a folder name target (to compile) inside “part3” folder. In package1 have a file name mygoal1.java. in package2 is my main file name program2.java. In my main file I do “Import package1.mygoal1” I meet these error when I run this:

javac -sourcepath "./part3" "./part3/package2/program2.java" -d "./target"


.part3package2program2.java:1: error: cannot access mygoal1
import package1.mygoal1;
 bad source file: .part3package1mygoal1.java
file does not contain class package1.mygoal1
Please remove or make sure it appears in the correct subdirectory of the sourcepath.

[enter image description here][1]

Sorry for my bad language, I’m not fluent in English, I just spent 20min to write this post, Hope and thank for any help [1]: https://i.stack.imgur.com/4xYL1.png

Advertisement

Answer

reading this error : file does not contain class package1.mygoal1 –>

I think that you are trying to “import” a non-existent class . import is a keyword in java used to import classes ( a java file can contain more than one class , but just one public class ), so in the file mygoal1.java you need to write a java class and give it the name “mygoal1” .

example :

    public class mygoal1{
    }

Later you will learn that import can be used to import static things (variables or methods) too ..

Advertisement