/**1、没有包结构(默认包)
===============================================================================
*/
public class HelloWorldV1 { public static void main(String[] args) { System.out.println("hello world v1 ....."); } }
D:\work\javacmd>javac HelloWorldV1.java
D:\work\javacmd>java HelloWorldV1
hello world v1 .....
D:\work>java HelloWorldV1
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldV1
D:\work>java -cp D:\work\javacmd\ HelloWorldV1
hello world v1 .....
D:\work\javacmd>javac -verbose HelloWorldV1.java
[解析开始时间 HelloWorldV1.java]
[解析已完成时间 11ms]
[源文件的搜索路径: .,C:\Program Files\Java\jdk1.6.0_39\lib,C:\Program Files\Jav
a\jdk1.6.0_39\lib\tools.jar]
[类文件的搜索路径: C:\Program Files\Java\jdk1.6.0_39\jre\lib\resources.jar,rt.jar,jsse.jar...tools.jar]
[正在装入 java\lang\Object.class(java\lang:Object.class)]
[正在装入 java\lang\String.class(java\lang:String.class)]
[正在检查 HelloWorldV1]
[正在装入 java\lang\System.class(java\lang:System.class)]
[正在装入 java\io\PrintStream.class(java\io:PrintStream.class)]
[正在装入 java\io\FilterOutputStream.class(java\io:FilterOutputStream.class)]
[正在装入 java\io\OutputStream.class(java\io:OutputStream.class)]
[已写入 HelloWorldV1.class]
[总时间 191ms]
win7下使用powershell,类似cmd,但可以使用shell命令
PS D:\work\javacmd> java -verbose HelloWorldV1 > r.txt
[Opened C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.Object from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.io.Serializable from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.Comparable from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.CharSequence from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.String from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.reflect.GenericDeclaration from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.reflect.Type from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.reflect.AnnotatedElement from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.Class from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.Cloneable from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.ClassLoader from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.ref.Reference,java.lang.reflect.*
...
[Opened C:\Program Files\Java\jdk1.6.0_39\jre\lib\charsets.jar]
...
[Loaded java.security.Principal from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded HelloWorldV1 from file:/D:/work/javacmd/]
hello world v1 .....
[Loaded java.lang.Shutdown from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\Program Files\Java\jdk1.6.0_39\jre\lib\rt.jar]
加载300+类
-classpath: 简写cp
-verbose: 冗长输出。它包括了每个所加载的类和每个所编译的源文件的有关信息。
-g: 生成所有的调试信息{包括vars-局部变量,缺省:{source-源文件,lines-行号}}
-g:none
-d: 设置生成的class文件的目录
PS D:\work\javacmd> javac -verbose -d ./bin HelloWorldV1.java
-sourcepath: 指定源文件路径,默认取classpath
PS D:\work> javac -verbose -d ./javacmd/bin -sourcepath javacmd javacmd/HelloWorldV1.java
PS D:\work> javac -verbose -d d:/work/javacmd/bin -sourcepath d:/work/javacmd javacmd/HelloWorldV1.java
/** 2、多个class文件。
===============================================================================
*/
package study.timeriver; public class ConsoleV3 { public static void print(String str) { System.out.println("Console.print: "+str); } public static void main(String[] args) { System.out.println("I am ConsoleV3....."); } }
package study.timeriver; public class StringUtilV3 { public static void print(String str) { ConsoleV3.print("StringUtil.print: "+str); } public static void main(String[] args) { System.out.println("I am StringUtilV3....."); } }
package study.timeriver; public class HelloWorldV3 { public static void main(String[] args) throws Exception { StringUtilV3.print("hello world ....."); Thread.sleep(1000000); } }
逐个编译class文件(3个文件:会自动编译关联的。但是不能运行。)
PS D:\work\javacmd> javac -d ./classes ./src/study/timeriver/ConsoleV3.java
PS D:\work\javacmd> java -cp ./classes study.timeriver.ConsoleV3
I am ConsoleV3.....
PS D:\work\javacmd> javac -cp ./classes -d ./classes ./src/study/timeriver/StringUtilV3.java
PS D:\work\javacmd> java -cp ./classes study.timeriver.StringUtilV3
I am StringUtilV3.....
PS D:\work\javacmd> javac -cp ./classes -d ./classes ./src/study/timeriver/HelloWorldV3.java
PS D:\work\javacmd> java -cp ./classes study.timeriver.HelloWorldV3
Console.print: StringUtil.print: hello world .....
打包根目录下面的文件:
jar cvf helloworldv3.jar ConsoleV3.class HelloWorldV3.class StringUtilV3.class
jar打包与直接压缩的区别:多了一个文件: META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.6.0_39 (Sun Microsystems Inc.)
打包指定目录(无效)
PS D:\work\javacmd> jar cvf helloworldv3.jar .\classes
执行jar包
PS D:\work\javacmd> java -jar .\helloworldv3.jar
Failed to load Main-Class manifest attribute from .\helloworldv3.jar
打包指定目录
PS D:\work\javacmd\classes> jar cvf helloworldv3.jar .\study
标明清单(manifest)
增加:study/(读入= 0) (写出= 0)(存储了 0%)
增加:study/timeriver/(读入= 0) (写出= 0)(存储了 0%)
增加:study/timeriver/ConsoleV3.class(读入= 689) (写出= 404)(压缩了 41%)
增加:study/timeriver/HelloWorldV3.class(读入= 393) (写出= 270)(压缩了 31%)
增加:study/timeriver/StringUtilV3.class(读入= 739) (写出= 429)(压缩了 41%)
修改META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.6.0_39 (Sun Microsystems Inc.)
Main-Class: study.timeriver.HelloWorldV3
PS D:\work\javacmd\classes> java -jar .\helloworldv3.jar
Console.print: StringUtil.print: hello world .....
/** 3、自定义bat批处理文件
===============================================================================
*/
使用bat启动:timeriver.bat
D:\work\javacmd\classes>timeriver.bat
D:\work\javacmd\classes>echo exceute jar....
exceute jar....
D:\work\javacmd\classes>start "tt" "C:\Program Files\Java\jdk1.6.0_39\bin\java"
-classpath "helloworldv3.jar" study.timeriver.HelloWorldV3
/** 4、tomcat的bat批处理文件
===============================================================================
*/
tomcat的catalina.bat
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
使用
echo _EXECJAVA.......................
echo %_EXECJAVA%
pause
goto end
输出变量。替换
start "hh" "C:\Program Files\Java\jdk1.6.0_39\bin\java" -Djava.util.logging.config.file="D:\tomcat\conf\logging.properties" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs="D:\tomcat\endorsed" -classpath "C:\Program Files\Java\jdk1.6.0_39\lib\tools.jar;D:\tomcat\bin\bootstrap.jar" -Dcatalina.base="D:\tomcat" -Dcatalina.home="D:\tomcat" -Djava.io.tmpdir="D:\tomcat\temp" org.apache.catalina.startup.Bootstrap
/** 5、-D参数,args参数
===============================================================================
*/
public class Test { public static void main(String[] args) { //eclipse中:args在Program arguments,-D在VM arguments if(args.length >= 2){ System.out.println(args[0]); System.out.println(args[1]); } System.out.println("args......"); System.out.println("-DmyConf="+System.getProperty("myConf"));; } }
D:\work\javacmd>java -DmyConf="conf/myConf.xml" Test wnj,ok good well
wnj,ok
good
args......
-DmyConf=conf/myConf.xml