全部博文(2759)
分类:
2012-11-17 14:08:53
原文地址:java命令行编译 运行JAVA类[class] 作者:escarp
代码:
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
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
备注: 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
将依赖的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
技能成功运行