代码:
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
- 示例代码
-
- package com.Javasoft;
- public class Hello{
- public static void main(String[] args){
- System.out.println("Hi ?");
- }
- }
- 目录结构
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
- 示例代码
- package com.Javasoft;
- import java.io.PrintStream;
- <span style="color: #ff0000;">import com.kettas.common.*;
- public class HelloInput{
- public static void main(String[]args){
- System.out.println("Hi ,what will you say? :");
- String str = <span style="color: #ff0000;">com.kettas.common.SystemIn.readString();
- System.out.println("You just told me that:");
- System.out.println(str);
- }
- }
备注: 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
技能成功运行