1. 程式人生 > >DOS或SH中執行java報錯Could not find the main class: XX. Program will exit.

DOS或SH中執行java報錯Could not find the main class: XX. Program will exit.

在命令列直接執行

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
後續又找了一下by phoo
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