2
我做了兩個文件,一個非常簡單的Java例子:編譯應用程序後無法找到主類
Dog.java包含:
package com.greg.dog;
public class Dog {
public void bark() {
System.out.println("bark");
}
}
TestDog.java包含:
package com.greg.dog;
public class TestDog {
public static void main(String args[]) {
Dog d = new Dog();
d.bark();
}
}
我把這兩個文件放在〜/ Desktop/test/src/com/greg/dog當我編譯它們的時候,Dog.java編譯沒事,但TestDog.java找不到我的狗類:
[email protected]:~/Desktop/test/src/com/greg/dog$ javac Dog.java
[email protected]:~/Desktop/test/src/com/greg/dog$ javac TestDog.java
TestDog.java:6: cannot find symbol
symbol : class Dog
location: class com.greg.dog.TestDog
Dog d = new Dog();
^
TestDog.java:6: cannot find symbol
symbol : class Dog
location: class com.greg.dog.TestDog
Dog d = new Dog();
^
2 errors
我在Ubuntu上,這裏的所有Java信息:
java version "1.6.0_0"
IcedTea6 1.3.1 (6b12-0ubuntu6.7) Runtime Environment (build 1.6.0_0-b12)
OpenJDK 64-Bit Server VM (build 1.6.0_0-b12, mixed mode)
任何想法我可能做錯了什麼?我需要導入Dog.java嗎?我在做包錯了嗎?
更新
每匿名的建議下,我改變目錄到src目錄和一切編譯。然而,當我去跑TestDog.java我得到這個錯誤:
[email protected]:~/Desktop/test/src$ java com/greg/dog/TestDog.java
Exception in thread "main" java.lang.NoClassDefFoundError: com/greg/dog/TestDog/java
Caused by: java.lang.ClassNotFoundException: com.greg.dog.TestDog.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: com/greg/dog/TestDog.java. Program will exit.
感謝。編譯都從該目錄工作,但是當我去運行它時,我得到一個類未找到錯誤。查看更新。 – Greg 2011-01-24 03:40:10