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?
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?
Use the command:
java -jar arquivo.jar
Or just double-click the .jar! file!
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
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.
Depends on jar There are two types of jar:
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