Use batch processing to execute java programs packaged as jar files

    After writing the java program, the user may need to type the command or use the idea tool to use it. In order to make it easy for the user to use, the batch command can be written in the file under the windows operating system, so that the user only needs to execute your batch processing. The file can execute your java program, isn't it cool to change from BS to CS immediately

    First, you need to write a java program, here is a simple small example

   1. Write a simple applet

The file path can be input into the program as a parameter, and the program can print the file path and generate the file

2. Export the executable jar package

The idea I used here exports the jar package, or you can use the command directly.

Here, exporting with idea needs to be executed in several steps

(1) Configure related options in idea

 

(2) Export

After the export is successful, you can find your executable jar file in the corresponding directory

3, then write the batch code

@echo off
cd %~dp0
cd..
set SERVER_HOME=%cd%
echo  %OPT%
echo  %SERVER_HOME%
java %OPT% -Dfile.encoding=UTF-8 -Duser.country=CN -Xmx512m -DSTOP.PORT=81003 -DSTOP.KEY=foo -Dserver.home=%SERVER_HOME% -cp %SERVER_HOME%/javaDemo/pearlJar.jar; com.web.bat.javaDemo.Main d:/logs/a.txt
pause

Mine looks like this when it's all done:

 

4. Click the batch file test.bat to execute

At the same time, the target directory will also generate the corresponding file a.txt

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297139&siteId=291194637