如何使用包名运行java类 [英] how to run the java class with has package name
本文介绍了如何使用包名运行java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个java类如下:
I have the two java class as follows:
1。
none package class
System.out.println("App1 hello world...");
2。
has package
package java.java.package1;
System.out.println("App1 hello world...");
然后,我编译并运行它们,结果如下:
then,I compile and run them,the result as follows:
D:\javaTest>javac App1.java
D:\javaTest>javac App2.java
D:\javaTest>java App1
App1 hello world...
D:\javaTest>java java.java.package1.App2
Exception in thread "main" java.lang.NoClassDefFoundError: java/java/package1/Ap
p2
Caused by: java.lang.ClassNotFoundException: java.java.package1.App2
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown 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: java.java.package1.App2. Program will exit.
那么如何运行App2类?
so how to run the App2 class?
推荐答案
您应该使用 -d
选项进行编译,以便这些类最终位于适当的目录层次结构中:
You should compile using the -d
option so that the classes end up in an appropriate directory hierarchy:
javac -d . App2.java
java java.java.package1.App2
此外,你不应该首先使用以 java
开头的包名。
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.
这篇关于如何使用包名运行java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文