Baidu Daniel visits Java late at night to start the exe program, pass parameters and get parameter operations

This article mainly introduces java launch exe program, transfer parameters and get parameter operation, it has a good reference value, I hope it will be helpful to everyone. Let's follow the editor to take a look

1. Start the exe program in java and add parameters

String[] cmd = {
    
    "hh.exe","12315"};
  
 Process process = null;
 try {
    
    
 ProcessBuilder pb = new ProcessBuilder();
 pb.command(cmd);
 process=pb.start(); 
 } catch (Exception e) {
    
    
 e.printStackTrace();
 }finally {
    
    
 if (process != null){
    
    
 process.destroy();
 }
 }

In cmd, the first parameter is to start the EXE program, and the second parameter is to pass parameters;
2. Get the passed parameters in the exe program.
The "hh.exe" program opened in step one is also based on java. .

We print the args[] of the main method to get the parameters passed by the previous program

public static void main(String args[]) {
    
    
// for (int i=0;i<args.length;i++){
    
    
 System.out.println(args[i]);
// JOptionPane.showMessageDialog (null, args[i], "日志", JOptionPane.CANCEL_OPTION);
// }
// System.exit(0);//结束 当前进程

**Supplement: java program generates executable exe file with parameters

Introduction
Recently I wrote a code generation tool that can automatically generate code for model, dao, service, serviceImpl, and controller layers according to some simple configurations, reducing the amount of redundant code development. After listening to the suggestions of colleagues, the tool was made into an executable tool, which can ensure the independence of the generation tool, and the tool does not depend on the original project code.

Environment configuration
-1, JDK environment

-2. Idea or Eclipse

-3. Download address of exe4j: https://www.ej-technologies.com/download/exe4j/files

**Steps

  • 1. Type the java program into a jar package (whether you are a web project or a java application). **

The way to type jar package in idea is: File->File Structrue->Atifacts->Select "+"Add jar(From modules with dependencies...), then select the project and click OK->Build->Build Atifacts->Build or Rebuid.

  • 2. Use exe4j to generate an exe executable file from the packaged jar package.

(1) Download and install exe4j, enter the bin folder of the installation directory, double-click to open "exe4j.exe"

(2) Click next and select "JAR in EXE" mode

(3) Click next, enter the name of the application to be generated and the path to generate the exe file

(4) Click next, click here 32-bit or 64-bit, if it is a 64-bit JDK environment, check "Generate 64-bit executable"

(5) Click next, enter the Executive name, and select Console application, which means that we run the generation tool in the form of a command line window

(6) Click next, click the green "+" on the right, select the generated jar file at Archive, and then select the class of the main program Main in the jar file

(7) Click next and enter the environment configuration of JRE

(8) Click next continuously to enter the following interface:

At this point, the exe file is successfully generated.
Run the
generated exe file in the output path set in the third step,

1. Enter the cmd command line window and enter the directory where the exe file is located.

2. Enter the name of the generated exe culture to execute the exe file, as in this example, enter "cg.exe" to run the file

Executable files that can be run with parameters
**Usually we want to specify configuration parameters at runtime. At this time, we only need to add spaces and parameters after "cg.exe", such as "cg.exe -t", in In the Main method of the java program: public static void main(String[] args) The args array can receive the parameters following the command line.
**
Notes

  • 1. If your JDK environment is 64-bit, remember to check "Generate 64-bit executable" in step (4)

  • 2. If you run the executable file, it will not output. It may be that you forgot to check the Console application in step (5).

The above is personal experience, I hope to give you a reference, and I hope you can support the editor for not easy creation. If an error or fully consider the place, look generous gift of teaching.

Guess you like

Origin blog.csdn.net/dcj19980805/article/details/115268932