标题: [其他] 批处理编译执行java源码 [打印本页]
作者: 小勇12 时间: 2010-1-29 13:28 标题: 批处理编译执行java源码
- ::添加当前目录下及其子目录下的所有jar到classpath中
- ::编译当前目录下及其子目录下的所有源文件
- ::根据源文件的包结构在bin下创建对应的目录,然后将生成的class文件放进对应的目录下
-
- @echo off&setlocal enabledelayedexpansion&&color 1f
- if exist build.lst del build.lst
- if exist bin rd /s /q bin
- md bin
- java -version
- if %errorlevel% neq 0 goto EnvAlert
- set line=TestClientTool coded by xiaoyong12
- set display=
- for /l %%i in (0,1,50) do (
- if not !dispaly! == !line! (set display=!display!!line:~%%i,1!) else ( set display= )
- title !display!
- ping/n 1 127.1>nul
- )
- echo/
- set line=请输入主类名(如com.test.Test):
- for /l %%i in (0,1,22) do (
- set/p=!line:~%%i,1!<nul
- ping/n 1 127.1>nul
- )
- ::添加所有jar文件到classpath中
- set /p class=
- set var=
- for /f %%i in ('dir /b /s %cd%') do (
- if "%%~xi" == ".jar" set var=!var!%%i;
- )
- set var=!var!.;
- set classpath=%classpath%;!var!
-
- cls
- ::编译所有java源文件,如果源文件有package结构,标志为0,没有package结构,标志为1
- for /f %%i in ('dir /b /s %cd%') do (
- if "%%~xi" == ".java" (
- find "package" %%i>nul
- if !errorlevel! equ 0 (
- for /f "tokens=1,2 skip=2" %%j in ('find "package" %%i') do (
- echo %%i 0 %%k>>build.lst
- )
-
- ) else (
- echo %%i 1 >>build.lst
- )
- )
-
- )
- set curdir=%cd%
- for /f "tokens=1,2,3" %%i in (build.lst) do (
- if %%j equ 1 (
- javac %%i
- if !errorlevel! neq 0 ( echo %%i文件编译发生错误>>error.lst ) else (
- move "%%~dpni.class" "!cd!\bin" )
- ) else (
- javac %%i
- if !errorlevel! neq 0 ( echo %%i文件编译发生错误>>error.lst ) else (
- set package=%%k
- set package=!package:;=!
- call :makedir !package!
- set package=!package:.=\!
- move "%%~dpni.class" "!curdir!\bin\!package!"
- cd /d !curdir!
- )
- )
- )
- cd /d !curdir!
- copy *.xml !curdir!\bin >nul 2>nul
- copy *.properties !curdir!\bin >nul 2>nul
- cd /d !curdir!/bin
- java %class%
- if %errorlevel% equ 0 goto stopped else (
- pause>nul )
-
- :alert
- cls
- for /l %%i in (1,1,8) do (
- echo.
- )
- echo 请确保%class%.java文件在当前目录下,并且所引用的所有jar包在当前/lib目录下,重新执行!
- ping /n 4 127.0.0.1>nul&exit
-
- :stopped
- ping/n 3 127.1>nul&echo/
- set line=执行结束,将要编译后类文件清除......
- for /l %%i in (0,1,20) do (
- set/p=!line:~%%i,1!<nul
- ping/n 1 127.1>nul
- )
- cd /d !curdir!
- del build.lst >nul
- echo Y|rd /s bin>nul && del /s /q /f *.class>nul 2>nul && ping/n 4 127.1>nul
- echo/&echo 按任意键退出......
- pause>nul
- exit
-
- :EnvAlert
- cls
- echo 请先安装java运行环境
- ping /n 4 127.0.0.1>nul&exit[code]::创建包结构目录
- :makedir
- set dir=%1
- cd /d !curdir!\bin
- :continue
- for /f "tokens=1,* delims=." %%i in ("!dir!") do (
- if not exist %%i md %%i
- cd %%i
- set dir=%%j&goto continue
- )
- goto :eof
复制代码
作者: yslyxqysl 时间: 2010-1-29 13:40
请加code标签
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |