在命令行直接运行
java Test
系统报错
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Test. Program will exit.
有一段时间未用过命令方式运行带package的class,重温一些命令。
package com.phoo;
public class Test {
public static void main(String[] args) {
System.out.println("hello world");
}
}
javac Test.java
这种编译必须进入到相应package目录内。
建议用javac -d ./ Test.java
运行时使用
java -cp ./ com.phoo.Test
javac - Java programming language compiler
http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html
java - the Java application launcher
http://docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html