How to execute .jar with the prompt?

10

I have already generated the jar file through Netbeans, but I am not able to run the program through the command prompt.

What command to run it in the Windows environment?

    
asked by anonymous 09.02.2014 / 20:03

4 answers

10

Use the command:

java -jar arquivo.jar

Or just double-click the .jar! file!

    
09.02.2014 / 20:16
8

To create an executable JAR it is necessary that the Manifest.mf of the same report the full name (package.class) of the class that contains the main() method that should be executed. Make sure this is the case. Open the JAR (it can be with a Unzip program) and see the contents of META-INF/Manifest.mf . It should contain the following line somewhere, with the package / name of your executable class.

Main-class: nome.do.pacote.ClasseExecutavel

In addition it is necessary that the executable class is there (see on / package name has a ClasseExecutavel.class )

Another problem that can make your JAR not run is the lack of dependencies. If your code has dependencies, it can work in NetBeans and it does not work on the command line if you do not inform the CLASSPATH of dependencies (JARs, directories, etc.). You can generate a JAR containing all dependencies, or run the JAR reporting the Classpath:

java -cp dependencia.jar:/tmp/dependencias -jar app.jar
    
10.02.2014 / 03:21
1

I believe that with the error presented, which indicates that the main class has not been defined, @helderdarocha's response is the one you are looking for:

  

To create an executable JAR it is necessary for the Manifest.mf of the same report to give the full name (package.class) of the class that contains the main () method that should be executed. Make sure this is the case.

In NetBeans this is even easier by editing the "Properties" of the project

It is unlikely that the JVM or its settings are in trouble, because it can execute the command in the Prompt and it returns a very clear error.

    
10.02.2014 / 16:56
1

Depends on jar There are two types of jar:

  • The jar executable

This is easy is only you to perform:

java -jar meuJar.jar

  • The jar package

    This jar can be executed but it was not born for it so the way to execute it is different, you need to say which is the executable class (the class that has the main method) / p>

    // OlaMundo.java
    import javax.swing.*;
    public class OlaMundo {
            public static void main(String args[]){
            JOptionPane.showMessageDialog(null, "Olá mundo");
        }
    }
    
    
    
    # compilando e rodando    
    javac OlaMundo.java
    jar cf olamundo.jar OlaMundo.class
    # no linux
    rm OlaMundo.class
    :: no windows
    del OlaMundo.class
    java -cp olamundo.jar OlaMundo
    
23.05.2015 / 02:44