java命令行编译 运行JAVA类[class]

时间:2021-10-11 17:43:30

代码:

package com.yanjiuyanjiu;

public class HelloWorld {

public static void main(String args[]) {

System.out.println(“Hello World!”);

}

}

执行:

d:\workspace\HelloWorld\bin\>java -classpath .; com/yanjiuyanjiu/HelloWorld

结果:

Hello World!


原文:http://cici-new-1987.iteye.com/blog/1377779

例子 1

 

  • 示例代码
  • Java代码    java命令行编译 运行JAVA类[class]
    1. package com.Javasoft;  
    2. public class Hello{  
    3.     public static void main(String[] args){  
    4.         System.out.println("Hi ?");  
    5.     }  
    6. }  
  • 目录结构

          mytestproject

                                |--  SRC

                                            |-- com

                                                        |--Javasoft

                                                                         |-- Hello.java

  • 编译开始

            进入命令窗口

             D:\mytestproject>javac -d classes src/com/Javasoft/Hello.java

             D:\mytestproject>

  • 运行

        D:\mytestproject>cd classes

        D:\mytestproject\classes>java com.Javasoft.Hello

        Hi ?

        D:\mytestproject>

 

 

 例子 2

  • 示例代码

   

Java代码    java命令行编译 运行JAVA类[class]
  1. package com.Javasoft;  
  2. import java.io.PrintStream;  
  3. <span style="color: #ff0000;">import com.kettas.common.*;  
  4. public class HelloInput{  
  5.     public static void main(String[]args){  
  6.         System.out.println("Hi ,what will you say? :");  
  7.         String str = <span style="color: #ff0000;">com.kettas.common.SystemIn.readString();  
  8.         System.out.println("You just told me that:");  
  9.         System.out.println(str);  
  10.     }  
  11. }  

  备注: HelloInput类,编译运行都依赖额外JAR文档(见附件)。

 

  •   下面是编译以及 运行都通过的方法

           将所依赖的JAR文档(kettas_common.jar)添加至环境变量 CLASSPATH之下

 

           变量名:CLASSPATH

           变量值:.;%JAVA_HOME%\lib;D:\mytestproject\lib\kettas_common.jar

            

           然后运行

           D:\mytestproject>cd classes

           D:\mytestproject>classes>java com.Javasoft.HelloInput

          

 

  •  下面是一次简单的尝试java命令行编译 运行JAVA类[class] 编译成功 运行失败

          

      将依赖的JAR包 copy至lib目录 D:\mytestproject\lib\kettas_common.jar

      编译:

     

D:\>cd mytestproject

D:\mytestproject>cd classes

D:\mytestproject\classes>java com/Javasoft/HelloInput.java
Exception in thread "main" java.lang.NoClassDefFoundError: com/Javasoft/HelloInp
ut/java
Caused by: java.lang.ClassNotFoundException: com.Javasoft.HelloInput.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: com/Javasoft/HelloInput.java.  Program will exit.
   

  • 下面是另一种尝试 成功运行

 

      将运行需用的SystemIn.class 文件拷贝至classes目录下

      新结构如下

       D:program\mytestproject

 

                            |--- classes

                                      |---Javasoft

                                               |-- com

                                                    |---.....

                                      |--kettas

                                               |--com

                                                                                                        |----.........

 

      D:\mytestproject\classes\java com.Javasoft.HelloInput

      技能成功运行