本身只是好奇,如何手工编译一个java项目,在项目开发中,我们不可能用手工命令行的形式。对于学好Java命令行是第一步。在这里我会逐渐引入bat,ant,maven,编译java项目最后发布。
第一步:编写java类:
package com.jsw.app;
public class Test {
public static void main(String[] args){
System.out.println("this is a test");
}
}
第二步:用命令行进行编译:
编译:
D:\project_test>javac -d . Test.java
执行:
D:\project_test>java com.jsw.app.Test
这是手工编译运行一个最简单的java程序。
问题总结:
但是在网上看到好多哥们出现这个异常:
D:\project_test>java com.jsw.app.Test
Exception in thread "main" java.lang.NoClassDefFoundError: com/jsw/app/Test
Caused by: java.lang.ClassNotFoundException: com.jsw.app.Test
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.jsw.app.Test. Program will exit.
就是因为我们在编译的时候没有指定class文件的路径,在运行时,jvm是根据你的包名来执行这个类。而我们默认的情况下class文件生成在当前目录,所以会出现此类异常。
当我们要引入第三方类库如何操作咧。
看命令:
编译:
D:\project_test\hellotest\src>javac -classpath D:\project_test\hellotest\lib\commons-logging-1.1.1.jar -d D:\project_test\hellotest\bin D:\project_test\hellotest\src\Test.java
运行:
将当前目录定位到:D:\project_test\hellotest\bin (注意一定要配置好你的CLASSPATH环境变量)
D:\project_test\hellotest\bin>java -classpath %CLASSPATH%;D:\project_test\hellotest\lib\ com.jsw.app.Test
如何手工编译一个项目基础就写到这里,下一步如何手工进行单元测试。