/home/thiru/VS Code Files/Java Files
├── F
│ ├── test.class
│ └── test.java
└── G
├── c.class
└── c.java
c.java imports F package and has main function
When I use javac and java :
:~/VS Code Files/Java Files/G$ javac -cp /home/thiru/VS\ Code\ Files/Java\ Files/ c.java
:~/VS Code Files/Java Files/G$ java -cp /home/thiru/VS\ Code\ Files/Java\ Files/ c
Error: Could not find or load main class c
Caused by: java.lang.ClassNotFoundException: c
When I simply use java command :
:~/VS Code Files/Java Files/G$ java -cp /home/thiru/VS\ Code\ Files/Java\ Files/ c.java
Thirumal
test.java :
package F;
public class test{
public static void d(){
System.out.println("Thirumal");
}
}
c.java :
import F.test;
public class c{
public static void main(String[] args){
test.d();
}
}
What's the difference?
c.javahas no package declaration, so there’s no reason forjava cto look inside theGdirectory. Usingjava sourcecode.javais an entirely different beast. You specified a source code file and the launcher will compile it and run the first class found in it, regardless of its name or package.javato run a class namec, because you specified-cp /home/thiru/VS\ Code\ Files/Java\ Files/so classes are resolved against that path.c(not having a package) can’t belong to a named module.