本人在准备OCJP的过程中,要考DOS下直接编译运行JAVA程序...但模拟了一个最简单的,运行时抛异常.我做法如下,请高手纠错...谢谢!第1步.写代码:package com.qp.ocjp;
public class TestJar {
public static void main(String[] args) {
new TestJar();
System.out.println("test Jar");

}第2步.上述代码存到如下目录下
  C:\Users\jocelynl\Desktop\development\java>
第3步:在上述目录下,手工创建包结构中需要的目录, com\qp\ocjp,用于存放编译后的class文件第4步: 在TestJar.java所在的目录下,用javac编译代码
    C:\Users\jocelynl\Desktop\development\java>javac TestJar.java
    编译后,检查第3步创建的目录下,生成了编译后的class文件第5步:java命令运行class文件-----出错了
     C:\Users\jocelynl\Desktop\development\java>java com.ocjp.qp.TestJar   Error: Could not find or load main class com.ocjp.qp.TestJarjavac

解决方案 »

  1.   

    第1步.写代码:
    ...
    第2步.上述代码存到如下目录下:C:\Users\jocelynl\Desktop\development\java>
    第3步.在这个目录下编译 javac -d . TestJar.java
    第4步.运行 java com.qp.ocjp.TestJar
      

  2.   

    javac 的时候带上-d参数就会自动创建包的目录。
      

  3.   

    javac -d . TestJar.java  楼主别忘了 中间有个点
      

  4.   

    运行时,写不写包名,都会出错。。C:\Users\jocelynl\Desktop\development\java>java TestJar
    Exception in thread "main" java.lang.NoClassDefFoundError: TestJar (wrong name:
    com/qp/ocjp/TestJar)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$100(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)C:\Users\jocelynl\Desktop\development\java>java com.qp.ocjp.TestJar
    Error: Could not find or load main class com.qp.ocjp.TestJar
      

  5.   


    运行java命令时带上classpath, java -cp . com.qp.ocjp.TestJar就可以了
      

  6.   


    运行java命令时带上classpath, java -cp . com.qp.ocjp.TestJar就可以了非常感谢,考试主要考classpath,我一直在等带这个参数的答案
      

  7.   

    前面的问题可以忽略了,请教下面的题目,我不明白-d -cp后面的点,代表什么?代表当前目录么?Given the following three files:
    2. package apollo;
    3. import apollo.modules.Lunar;
    4. public class Saturn {
    5. public static void main(String[] args){
    6. Lunar lunarModule = new Lunar();
    7. System.out.println(lunarModule);
    8. } }
    2. package apollo.modules;
    3. public interface Module { /* more code */ }
    2. package apollo.modules;
    3. public class Lunar implements Module { /* more code */ }
    And given that Module.java and Lunar.java were successfully compiled and the directory
    structure is shown below:
    $ROOT
    |-- apollo
      | |-- modules
        | |-- Lunar.class
    |-- controls.jar
      | |-- apollo
        | |-- modules
          | |-- Module.class
    |-- Saturn.java
    Which are correct about compiling and running the Saturn class from the $ROOT directory?
    (Choose all that apply.)
    A. The command for compiling is javac -d . -cp . Saturn.java
    B. The command for compiling is javac -d . -cp controls.jar Saturn.java
    C. The command for compiling is javac -d . -cp .:controls.jar Saturn.java
    D. The command for running is java -cp . apollo.Saturn
    E. The command for running is java -cp controls.jar apollo.Saturn
    F. The command for running is java -cp .:controls.jar apollo.Saturn
    G. The command for running is java -cp controls.jar -cp . apollo.Saturn答案是:ACF
      

  8.   

    命令行中编译与运行带有包的java文件
    http://afantihust.blog.51cto.com/2231549/684608
      

  9.   

     -cp <路径>                   指定查找用户类文件和注释处理程序的位置
     -d <目录>                    指定放置生成的类文件的位置
    如果你对javac 编译命令有任何的疑问的话,在控制台输入javac回车看下,里面应该会有你想要的...