我有一個代碼如下。我可以在NetBeans中正常編譯和運行它。但對於javac/java,我無法正常運行。我錯過了什麼?如何通過javac/java編譯/運行java代碼?
代碼:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/* the original code is from link: http://edu.qudong.com/safe/base/Javascript/shilidaima/20080514/12527.html */
package gendemo;
/**
*
* @author tomxue
*/
class Gen2 {
private Object ob; //定義一個通用類型成員
public Gen2(Object ob) {
this.ob = ob;
}
public Object getOb() {
return ob;
}
public void setOb(Object ob) {
this.ob = ob;
}
public void showTyep() {
System.out.println("T的實際類型是: " + ob.getClass().getName());
}
}
public class GenDemo2 {
public static void main(String[] args) {
//定義類Gen2的一個Integer版本
Gen2 intOb = new Gen2(new Integer(88));
intOb.showTyep();
int i = (Integer) intOb.getOb();
System.out.println("value= " + i);
System.out.println("----------------------------------");
//定義類Gen2的一個String版本
Gen2 strOb = new Gen2("Hello Gen!");
strOb.showTyep();
String s = (String) strOb.getOb();
System.out.println("value= " + s);
}
}
通過的javac,編譯後,我得到了下面的結果。
[email protected]:~/test$ javac GenDemo2.java
[email protected]:~/test$ ls
Gen2.class GenDemo2.class GenDemo2.java
然後,如果我運行這樣的:
[email protected]:~/test$ java Gen2
Exception in thread "main" java.lang.NoClassDefFoundError: Gen2
Caused by: java.lang.ClassNotFoundException: Gen2
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
[email protected]:~/test$ java GenDemo2
Exception in thread "main" java.lang.NoClassDefFoundError: GenDemo2
Caused by: java.lang.ClassNotFoundException: GenDemo2
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
有什麼不對呢?
編譯:javac的''
你嘗試過'java GenDemo2'嗎? –