2012-03-04 137 views
0

當我導出我的項目時,它可以工作,但是當我打開它時,它會顯示:「找不到主要類:Game.Frame」,並且有一個文件夾Game和在那裏名爲FrameFrame.class是一流的班。嘗試運行從Eclipse導出的jar文件時出錯

+0

你怎麼「打開」它?你的意思是從一個罐子裏跑?請參閱http://docs.oracle.com/javase/tutorial/deployment/jar/run.html – 2012-03-04 21:44:43

回答

0

聽起來好像你的「遊戲」類在一個包中,而你沒有正確指向包。

例:

http://www.jarticles.com/package/package_eng.html

If you try to run this HelloWorld using java HelloWorld, you will get the following error:

C:\world>java HelloWorld 
Exception in thread "main" 
java.lang.NoClassDefFoundError: HelloWorld (wrong name: world/HelloWorld) 
     at java.lang.ClassLoader.defineClass0(Native Method) 
     at java.lang.ClassLoader.defineClass(ClassLoader.java:442) 
     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:101) 
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:248) 
     at java.net.URLClassLoader.access$1(URLClassLoader.java:216) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:197) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:191) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:290) 
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 

The reason is right now the HelloWorld class belongs to the package world. If we want to run it, we have to tell JVM about its fully-qualified class name (world.HelloWorld) instead of its plain class name (HelloWorld).

C:\world>java world.HelloWorld 
C:\world>Hello World 

Note: fully-qualified class name is the name of the java class that includes its package name

注意,那就是,.jar文件也有類似的規則 - 如果你有一個包,你需要限定包範圍。是的,你應該總是使用「包」(這是道德相當於.Net稱爲「命名空間」)。