Pass parameters to jar file

Asked

Viewed 992 times

2

I have an example project ready (very simple), however I would like to make a connection configurator with the database. Once compiled, the system creates a.jar file and I would like to know how to pass a parameter in the executable to call the database configurator (Ex: .jar file /db).

2 answers

1


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

To illustrate, I have elaborated 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);

    }

}

Note that I am scanning the array received by the main and concatenating a variable to the part, and then displaying. 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:

inserir a descrição da imagem aqui

It is worth noting that the parameters should be separated by space, if you have some parameter that contains space (such as file names or paths, for example), it should be passed between double quotes.


Related Topics:

  • It helped a lot, I made it work. Thanks.

0

Following your tip, I got with 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);
    }
}
  • If my answer helped you, you can accept it instead of replicating. See how the site works in the following link: https://answall.com/tour

Browser other questions tagged

You are not signed in. Login or sign up in order to post.