当前目录下有个test文件夹,test下面有个games.jar,games.jar里有个包games,包里有个类games.Chess[Xxxx ~]$ java -classpath test/* games.Chess
Exception in thread "main" java.lang.NoClassDefFoundError: test/MainClass/java~
Caused by: java.lang.ClassNotFoundException: test.MainClass.java~
        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:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: test/MainClass.java~.  Program will exit.[Xxxx ~]$ java -classpath test games.Chess
Exception in thread "main" java.lang.NoClassDefFoundError: games/Chess
Caused by: java.lang.ClassNotFoundException: games.Chess
        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:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: games.Chess.  Program will exit.[Xxxx ~]$ java -classpath test:test/* games.Chess
chess........[Xxxx ~]$ java -classpath test/*:download games.Chess
chess........[Xxxx ~]$ java -classpath /etc:test/* games.Chess
chess........[Xxxx ~]$ java -classpath test/*: games.Chess
chess........[Xxxx ~]$ java -classpath :test/* games.Chess
chess........问题:为什么java -classpath test/* games.Chess 不可以?干嘛非要加个别的路径?
文档上可没说要这么干阿
Understanding class path wildcards    Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. Files will be considered regardless of whether or not they are hidden (that is, have names beginning with '.').    A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo:foo/* or foo/*:foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.

解决方案 »

  1.   

    不一定要这样呀。
    只要你的环境变量里面设定了你jre的路径,只要把
    你的编译文件路径搞对了,就可以了
      

  2.   

    运行jar文件的命令是 java -jar test/games.jar
    你的jar文件里要设置main-class属性才能运行
      

  3.   

    我不是运行jar文件 我是设置classpath
      

  4.   


    不一定啊,这个很灵活,您可以自己设定自己运行环境,帮您引用了篇文章,看看CLASSPATH是什么?它的作用是什么?
        它是Javac编译器的一个环境变量。
        它的作用与import、package关键字有关。
        当你写下improt java.util.*时,编译器面对import关键字时,就知道你要引入java.util这个package中的类;但是编译器如何知道你把这个package放在哪里了呢?所以你首先得告诉编译器这个package的所在位置;如何告诉它呢?就是设置CLASSPATH啦 :) 如果java.util这个package在c:\JDK\ 目录下,你得把c:\jdk\这个路径设置到CLASSPATH中去!当编译器面对import java.util.*这个语句时,它先会查找CLASSPATH所指定的目录,并检视子目录java\util是否存在,然后找出名称吻合的已编译文件(.class文件)。如果没有找到就会报错!
        CLASSPATH有点像c\C++编译器中的INCLUDE路径的设置哦,是不是?当c\c++编译器遇到include <iostream>这样的语句,它是如何运作的?哦,其实道理都差不多!搜索INCLUDE路径,检视文件!
        当你自己开发一个package时,然后想要用这个package中的类;自然,你也得把这个package所在的目录设置到CLASSPATH中去!
        CLASSPATH的设定,对JAVA的初学者而言是一件棘手的事。所以Sun让JAVA2的JDK更聪明一些。你会发现,在你安装之后,即使完全没有设定CLASSPATH,你仍然能够编译基本的JAVA程序,并且加以执行。