Pass parameters to jar file

2

I have an example project ready (very simple), but I would like to make a database connection configurator. After compiling, the system creates a .jar file and I would like to know how to pass a parameter on the executable to call the database configurator (eg .jar / db file).

    
asked by anonymous 12.05.2018 / 20:53

2 answers

2

If the goal is to pass parameters to the jar, it is enough to treat the array of Strings that the main method of the main class receives, because it is through it that external commands execute the execution of the jar in the virtual machine. >

To illustrate, I did the following example:

import javax.swing.JOptionPane;

public class MainTeste {

    public static void main(String[] args) {


        String commands = "";

        for(String str : args) {
            commands += str;
        }

        JOptionPane.showMessageDialog(null, "Foram passados os seguintes comandos: " + commands);

    }

}

See that I'm scanning the array received by main and concatenating a variable in the part, and then displaying it. After generating a. jar , just pass the parameters through a command line similar to below:

java -jar seuJarFile.jar comando1 comando2 comando3

See the code above, after generating the jar, working:

Notethattheparametersmustbeseparatedbyspace,ifyouhaveanyparametersthatcontainspace(suchasfilenamesorpaths,forexample),mustbeenclosedindoublequotationmarks.

Relatedtopics:

12.05.2018 / 21:11
0

Following your tip, I got the code below.

public static void main(String[] args) {

    String commands = "";

    for(String str : args) {
        commands += str;
    }

    if (commands.equals("db")) {
        TelaConfigDB.main(null);
    } else {
        TelaSplash.main(null);
    }
}
    
12.05.2018 / 22:01