gezhonglunta gezhonglunta - 1 year ago 113
Java Question

how to run the java class with has package name

I have the two java class as follows:


none package class
System.out.println("App1 hello world...");


has package

System.out.println("App1 hello world...");

then,I compile and run them,the result as follows:



D:\javaTest>java App1
App1 hello world...


Exception in thread "main" java.lang.NoClassDefFoundError: java/java/package1/Ap
Caused by: java.lang.ClassNotFoundException:
at$ Source)
at Method)
at Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Program will exit.

so how to run the App2 class?

Answer Source

You should compile using the -d option so that the classes end up in an appropriate directory hierarchy:

javac -d .

Also, you shouldn't use a package name starting with java in the first place.

Also, if you put the source in an appropriate directory hierarchy, and compile/run from the root of the hierarchy, you wouldn't have this problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download