Posted By

s1w on 10/25/14


Tagged

java dir output compile javac


Versions (?)

compile java into output dir >> DOS batch


 / Published in: DOS Batch
 

URL: http://siwego.net/dev/compile.bat

Tired of mess in project directory after single compilation process? Just drop this batch into java/bin location and modify jdk compiler path in any simple Java IDE (like JCreator LE in its best fastest ver 2.5) to "$[JavaHome]\bin\compile.bat" with parameters for both compiler and run application -cp output $[JavaClass]

This batch is adjusted for fast edit and run single java files, demos, examples and ad hoc computations; although for bigger projects it is also possible to configure IDE to use this bypass: just create a new Tool with command CMD.EXE and args "$[JavaHome]\bin\compile.bat" -d output %proj%.java for compile and second Tool to set active project with command CMD.EXE and args setx proj <proj> (with prompt for arguments option checked).

  1. @echo off
  2. SET output_new="output" dir created
  3. SET output_err="output" dir cant be created, because there is a file in current dir having the same name
  4. SET output_wut=an exception occured while creating "output" dir
  5. SET _output=
  6. SET _found=
  7. rem echo processing s1w's compile output..
  8. rem if "%~1" NEQ "" echo parametry: %*
  9. SET _params=%*
  10.  
  11. FOR %%G IN (%*) DO (
  12. IF DEFINED _found (
  13. call :pTrim %%G _output
  14. call :clearParams %%G
  15. goto :check dir
  16. )
  17. IF /I [%%G]==[-d] (
  18. SET _found=true
  19. )
  20. )
  21. call :makedir "%CD%\output" _output
  22. goto :eof
  23.  
  24.  
  25. :check dir
  26. SET _lastdir=
  27. SET _path=
  28. call :lastdir %_output% _lastdir
  29. call :path %_output% _path
  30. IF NOT EXIST %_path% goto :not_dir
  31. IF NOT EXIST %_output% (
  32. if "%_lastdir%"=="output" (
  33. call :makedir %_output% _output
  34. goto :eof
  35. )
  36. :not_dir
  37. echo bledny folder docelowy! %_output%
  38. goto :err
  39. ) ELSE (
  40. if "%_lastdir%"=="output" (
  41. cmd /c cd %_output% 2>nul && (goto :compile) || (echo %output_err%&&goto :err)
  42. )
  43. cmd /c cd %_output% 2>nul || (goto :not_dir)
  44. call :makedir %_output%\output _output
  45. goto :eof
  46. )
  47. ::below command is simpler, but \nul is not working with long pathes or within quotes
  48. rem IF NOT EXIST %_output%\output\nul MD %_output%\output
  49.  
  50.  
  51. :makedir
  52. IF "%~2" NEQ "" SET %~2=%1
  53. md %1 2>nul && (echo %output_new%&&goto :compile)
  54. IF EXIST %1 (
  55. cmd /c cd %1 2>nul && (goto :compile) || (echo %output_err%)
  56. ) ELSE (
  57. echo %output_wut%
  58. )
  59. GOTO :err
  60.  
  61.  
  62. :compile
  63. rem set _
  64. IF EXIST "%~dp0javac.exe" (
  65. "%~dp0javac" -d %_output% %_params%
  66. echo processed by s1w's batch
  67. ) ELSE (
  68. echo cannot find javac.exe, you must either update path entry for jdk\bin or reinstall JDK environment
  69. )
  70. GOTO :success
  71.  
  72.  
  73. :lastdir
  74. IF "%~2" NEQ "" SET %~2=%~n1
  75. exit /b
  76.  
  77. :path
  78. IF "%~2" NEQ "" SET %~2="%~dp1"
  79. exit /b
  80.  
  81.  
  82. :clearParams path
  83. SETLOCAL EnableDelayedExpansion
  84. set _tmp=!_params:*-d=-d!
  85. set _l=%1
  86. for /f "tokens=1* delims=%_l:~0,1%" %%a in ("!_tmp!") do call set _d=%%a
  87. rem echo do usuniecia [!_d!%1]
  88. call set _params=%%_params:!_d!%1=%%
  89. call :sTrim _params
  90. ENDLOCAL & SET _params=%_params%
  91. EXIT /b
  92.  
  93.  
  94. :pTrim string out -- strips white spaces (or other characters) from the path
  95. :$source http://www.dostips.com - s1w mod
  96. call set string=%%~1%
  97. SETLOCAL EnableDelayedExpansion
  98. set max=32
  99. for /f "tokens=*" %%a in ("!string!") do set "string=%%a"
  100. for /l %%a in (1,1,%max%) do (
  101. set _finish=true
  102. if "!string:~-1!"==" " set string=!string:~0,-1!
  103. if "!string:~-1!"=="\" set string=!string:~0,-1!
  104. if "!string:~-1!"=="/" set string=!string:~0,-1!
  105. if "!string:~-1!"==" " set _finish=false
  106. if "!string:~-1!"=="\" set _finish=false
  107. if "!string:~-1!"=="/" set _finish=false
  108. if !_finish!==true goto :loopend
  109. )
  110. :loopend
  111. ( ENDLOCAL & REM RETURN VALUES
  112. IF "%~2" NEQ "" (
  113. SET %~2="%string%"
  114. ) ELSE (
  115. IF "%~1" NEQ "" SET %~1="%string%"
  116. )
  117. )
  118. EXIT /b
  119.  
  120.  
  121. :sTrim string -- strips white spaces from the string - s1w
  122. SETLOCAL EnableDelayedExpansion
  123. call set string=%%%1%%
  124. set max=32
  125. for /f "tokens=*" %%a in ("!string!") do set "string=%%a"
  126. for /l %%a in (1,1,%max%) do (
  127. if "!string:~-1!"==" " set string=!string:~0,-1!
  128. if NOT "!string:~-1!"==" " goto :loopend
  129. )
  130. :loopend
  131. ( ENDLOCAL & REM RETURN VALUES
  132. IF "%~1" NEQ "" SET %~1=%string%
  133. )
  134. EXIT /b
  135.  
  136.  
  137. :err
  138. echo compilation suppressed
  139. :success
  140. EXIT /b

Report this snippet  

You need to login to post a comment.