Executable jar in eclipse is not runnable - java

After I finished making a program in java on eclipse, and decided to make a runnable jar I went through all the correct processes and created it. When I tried to run it later, I clicked on it, but nothing happened! Is this because I only have one class or do I need to do somthing else?
Ps. It runs perfectly fine in eclipse.

run it from command prompt, if it just outputs to console then you may not see anything by double click run java -jar xxxx.jar

Normally a JAR file have manifesto file attached to it which hold the class file which contains main method.If you have created your program in eclipse with main method in the selected class and created jar file than it won't make any issue.Anyways re-create it by simply clean and build,go through the dist folder in your project it will have an executable jar file ready for you.
For console application you wont be able to see the output directly on the windows screen but only on command prompt by running the file by java -jar jarfilename.Else any frame or swing application will run atomatically on windows screen by double click.

Use the command java -cp yourjar.jar com.example.Main where yourjar.jar is the name of your jar file and com.example.Main is the full name of the class containing your main method( including the package name)

Related

How can I start a non-runnable Java file (.jar) with a console (cmd) open to display outputs without using something like mvn exec:java

I know there's threads similar to this one have been posted before but after reading through many of them, I still haven't found my answer.
What I have is a .jar file and I want to be able to run it with a command line output. I've tried java -jar .jar but it just closes the window instantly and I don't want to use mvn exec:java as I need to just have the jar file without pom.xml and also not need to have maven installed on the running machine.
Any ideas of how to do this?
You need to run the program indicating the jar in the classpath.
Assuming you are in the folder with the jar:
java -cp myjar.jar mypackage.MainClass
The reason the window closes immediately is because with the -jar option, java will look for a manifest file to know which is the main class. As the jar is not executable, java prints a message saying it didn't find a manifest file and exits, which closes the window.

Export runnable jar files

Need to export clickable and executable file in eclipse. Simple exporting does not launch the program. How to get a clickable jar?
You should click file, then click export, then click java and save it as runnable jar file.
To run the .jar file, open cmd or terminal, make sure you are in the right path and
type java -jar <jar-file-name>.jar
Note if you cant run it, mostly its your file path not correct
Try to go in brief using the below link :
http://www.wikihow.com/Create-an-Executable-File-from-Eclipse
The code listing represents a command prompt application; you can only make it do anything when physically run on the command prompt / shell itself, especially because it expects user input that way. The reason why turning the given program into an executable jar will result in "nothing" happening when you double click it, is because under Windows the default binding for the 'jar' filetype will be setup to run through javaw.exe - and javaw.exe is designed to not open up any command prompt.
In order to make a program be able to run without the command prompt so you only have to double click on it, you will need to get rid of the command prompt interface and instead build a proper graphical user interface, using for example JavaFX or Swing.

Jar not starting on double click, but starts from command line

I'm using IntelliJ to create JAR file, and it executes normally when run from command line, but on double clicking it, nothing happens. The JAR I export from eclipse work normally, and since I'm a beginner using IntelliJ my guess is that I am doing something wrong.
I'm exporting the JAR in this way:
Project structure -> jar -> from modules and dependencies then use this
configuration to build.
Not sure why everyone is playing dumb about self executing jars.
Just add a MANIFEST.MF file and specify the fully qualified main class as Main-Class: my.package.MyClass
Also, make sure your executable type for .jar file type is Java
Once you do this you can just double click the jar to execute it.
Intellij will not execute a JAR for you. Remember JAR is a container, Is the same as if you were asking to execute a ZIP file.
What I am sure you want is to execute a class inside your jar file. Which would be the class which has the:
public static void main(String[] args) { ... }
You have two options:
1) execute the jar from command Line
2) right click on the "main" word I wrote above and then press RUN.. You will see a small green arrow (play button). That is the only way of running a class inside Intellij.
Hope it helps
If your JAR starts from the command line using jar -jar <file>, that's as executable as a JAR file gets (in other words, metadata specifies where the main class is). You can if you want use the OS provided tools to specify that when you double click on a .jar file, it should execute java -jar <file>, but that's beyond the scope of JAR files. If you're running Windows for example, you should be able to achieve this by following the procedure outlined here. Again, this is OS specific and may even be specific to which version you're running, and is essentially just a fancy wrapper around the double-click event to execute the java -jar command.
Make sure that the x-flag is set for the jar.
java -jar xyz.jar
does not need that, but double click need it:
ls -l xyz.jar
chmod 755 xyz.jar

Cannot find main class, java error

I have been testing a java swing program that I have been making. On my one computer, the one I originally made it on, it works fine. I have tested the same program on 3 different computers and it runs when I launch it out of the ide, but when I double click the jar I get a popup error window titled 'Java Virtual machien Launcher'. The error is "Could not find the main class: xxxxxxx. Program will exit."
I cannot figure out for the life of me what is going on. It was working before.
You need to include a Manifest file within your jar. In this, you specify which class is to be used as the entry point when the jar gets launched.
Create a file called Manifest.txt, and add:
Main-Class: yourMainClass.class
Then, to create the jar :
jar cfm JarName.jar Manifest.txt yourMainClass/*.class
To run the from the command line, use :
java -jar JarName.jar

System.out not working when calling jar-file from Windows command line

I have this class:
public class Test {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
And I used Eclipse's "Export to runnable JAR" function to make it into a executable jar-file (test.jar). When I run this program in Eclipse, it prints out "Hello World!", but when I call the jar-file from the command line, nothing happens.
I have tried calling the jar-file like this:
test.jar
test.jar -jar
There is no output whatsoever.
I have another program that also has a side effect (in addition to output to stdout) and while the side effect is being performed (which tells me the jar-file was definitely executed), again no output is given to the command line. I have also tried using stderr, but that makes no difference. Does anybody know how I can make this work?
Thanks!
You must run the JAR using
java -jar test.jar
(Your JRE's bin folder must be added to PATH in order to get the command working from any location)
NOTE: I know you created the JAR using Eclipse but you might want to know how does an executable JAR works
The previous answers are correct, so I'll just clarify a bit the "executable jar" concept.
There's no such thing as an "executable jar", equivalent to an exe file in windows, in an "executable jar" you'll be only specifying the "entry" point ( your main class you want to be executed by default )
A jar is basically just an archive, you'll still need java to actually launch the jar ( java -jar your.jar )
The fact that in windows you might have an association with javaw.exe means this will be launched by the OS when you double-click on the jar, like a .txt file being opened automatically with notepad, it doesn't make the .txt an executable file.
Check this out as well :
JAR files revealed
When you invoke the jar using 'test.jar' the starting of the app is handed off to the registered java handler for jar files, which doesn't run in the context of the command line.
The default jar handler doesn't open console based System.{out,err} file handles, as it would mean a cmd style window for each of the jar files launched, which is not an ideal situation.
The previous answer, using java -jar test.jar causes it to run within the context of the current cmd window, and thus you will see the output.

Categories

Resources