This is follow-up on my question:
error in java basic test program
This is still confusing me.
So specific problem is:
I already have a package and directory done.
say com.learn.java.mypackage
$ pwd
.../com/learn/java/mypackage
and here I want to create a test program in the same package and execute it.
$cat TestPackage.java
package com.learn.java.mypackage;
public class TestPackage
{
public static void main(String args[])
{
System.out.println("Hello World\n");
}
}
$ javac TestPackage.java
// runs file
$ Java TestPackage
Exception in thread "main" java.lang.NoClassDefFoundError: TestPackage (wrong name: com/learn/java/TestPackage)
if java runtime wants to make sure that a file belonging to a package lives in the same named directory, it is true here. Then why does it still crib?
You should do
java com.learn.java.mypackage.TestPackagefrom within the parent folder ofcom.So if
com‘s full path is/my/project/folder/com/...you should calljava com.learn.java.mypackage.TestPackagefrom/my/project/folderPasted in from the chat:
You can use from anywhere
java -cp /my/project/folder a.b.c.ClassName, which tellsjavato look for classClassNamefound in the packagesa.b.c, and the packages should be searched at the path:/my/project/folder