How to run . jar with prompt?

Asked

Viewed 77,025 times

12

I’ve managed the jar file for Netbeans, but I’m unable to run the program at the command prompt.

What command to run it in Windows environment?

4 answers

12

Use the command:

java -jar arquivo.jar

inserir a descrição da imagem aqui


Or just double-click on the file. jar!

  • Error: Unable to locate or load main class...

  • Test with helloWorldSimples.jar. If it doesn’t work yet, try reinstalling JRE (http://www.oracle.com/technetwork/java/javase/downloads/index.html).

  • When the installed version of java is lower than the jar version, this error appears. Install a newer version of java.

8

To create an executable JAR it is necessary that the Manifest.mf of it enter the full name (package.Class) of the class containing the method main() which must be executed. Check if this is the case. Open the JAR (can be with an Unzip program) and see the contents of META-INF/Manifest.mf. It must contain the following line somewhere, with the package/name of its executable class.

Main-class: nome.do.pacote.ClasseExecutavel

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

Another problem that can cause your JAR to not run is the lack of dependencies. If your code has dependencies, it may work in Netbeans and not run on the command line if you do not inform the CLASSPATH of the dependencies (Jars, directories, etc.) You can generate a JAR containing all the dependencies, or run the JAR by informing the Classpath:

java -cp dependencia.jar:/tmp/dependencias -jar app.jar

2

Depends on the jar There are two types of jar:

  • The jar executable

This is easy if you just run:

java -jar meuJar.jar

  • The jar bundle

    This jar can be executed but not born for it so the way to execute it is different, you need to tell which is the executable class (the class that has the method main) ex:

    // 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
    

1

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

To create an executable JAR it is necessary that the Manifest.mf of the same enter the full name (package.Class) of the class that contains the main() method that must be executed. Check if this is the case.

In Netbeans this is even easier by editing the project’s "Properties".

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

Browser other questions tagged

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