Java 手工试玩路线
编译与运行
编译:
javac -sourcepath src -d target src/Main.java
运行:
cd target java Main
或:
java -classpath target Main
JAR
打包:
jar -cf news.jar news
编译(Windows):
javac -sourcepath src -cp lib/news.jar -d target src/Main.java
运行(Windows):
java -cp .;../lib/news.jar Main java -cp target;lib/news.jar Main
脚本
Windows
批处理文件 build.bat
:
@echo off setlocal enabledelayedexpansion del target\* /s /q rd target /s /q md target echo target clean successfully set jars=. for %%i in (lib\*.jar) do ( ::echo dependency: %%i set jars=!jars!;%%i ) echo jars: %jars% javac -sourcepath src -cp %jars% -d target src\Main.java java -cp target;%jars% Main
执行:
build
Linux 脚本
Shell 脚本 build
:
#!/bin/bash rm -rf target mkdir target echo target clean successfully jars=. for file in lib/*.jar; do #echo dependency: $file jars=$jars:$file done echo jars: $jars javac -sourcepath src -cp $jars -d target src/Main.java java -cp target:$jars Main
chmod +x build
执行:
./build
Ant
build.xml
:
<?xml version="1.0" encoding="UTF-8"?> <project name="myjava" basedir="." default="run"> <property name="src" location="src"/> <property name="res" location="resource"/> <property name="lib" location="lib"/> <property name="build" location="target"/> <property name="main-class" value="Main"/> <path> <fileset dir="${lib}" includes="*.jar"/> </path> <target name="init" depends="clean"> <mkdir dir="${build}"/> <echo message="target clean successfully" /> </target> <target name="compile" depends="init,cp"> <javac srcdir="${src}" destdir="${build}" classpathref="jars" includeantruntime="false"/> </target> <target name="run" depends="compile"> <java classname="${main-class}"> <classpath> <pathelement location="${build}"/> <path refid="jars"/> </classpath> </java> </target> <target name="cp"> <copy todir="${build}"> <fileset dir="${res}"> <include name="**"/> </fileset> </copy> </target> <target name="clean"> <delete dir="${build}"/> </target> </project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论