Java Program only runs in IDE, not exported as a Jar - java

I can run my software without any problems in my IDE, but when I try to compile it to a jar, and try to run it, it says that the main class can't be found. I succeeded to make it work perfectly several times in the past, but I found this problem few time ago, so I can't really know when it went wrong.
The jar has a META-INF, all properly set up, like in my older versions, and the main class is there. Other peoples can't compile too, so it's not just me. The code is on Github, so that could be a tool to help me solve this.
But I want to know, just by reading this, is there some common mistakes that could cause this, or this is uncommon?
EDIT:
I'm using IntelliJ IDEA
and I will check if the versions are the same.
EDIT2:
They do have the same version, and it's exporting an executable jar with the correct main class defined, as I have beeing doing for all the previous versions.

Check that the MANIFEST.MF is present in the META-INF folder of the jar, and make sure that the line :
Main-Class: yourMainClass
is present inside the file, then make sure that you start the jar with this command line :
java -jar yourjar.jar
Have you tried to launch the jar file specifying the main class ? For example :
java yourjar.jar yourMainClass
Does it work in this way ?

Related

Program runs in Eclipse, but exported runnable jar file will not open?

It looks like this is a common problem, but none of the previous posts seem to address my issue.
I believe I've narrowed it down to one problem. Any application that uses an InputStream will not open, but all my other applications run fine.
The application runs fine in Eclipse, but the window won't even open when I try to run the jar file.
Task manager shows it pop up for about a second or two, and then disappears.
I have tried all three options for the Library handling upon exporting and none of them fix the issue.
Can anyone explain this?
Run it from the command line. This will allow you to see the exception that's thrown that's preventing your program from progressing.
java -jar YourJar.jar
I know I'm late, but I want to prevent people having to search hours for the same Error I just did. In Eclipse the path-String isn't case-sensitive. In the exported Runnable Jar File it is. So make sure all the pathsnames have the capital letters in the right positions, or to be save don't have capital letters at all.
Setup Your Manifest
If I had to guess, your manifest doesn't contain your main class OR the classpath is not defined.
Main-Class: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Class-Path: https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
If you're using a build tool like maven, it needs to be configured to add these properties: https://maven.apache.org/shared/maven-archiver/examples/classpath.html
Execute Your Jar
There are two ways to run an executable jar. As an executable jar you need to define the Main-Class and Class-Path in your manifest:
java -jar YourJar.jar
You can also skip setting up your manifest and define your classpath and main class through the JVM's parameters.
java -cp=${PATH_TO_JAR} main.package.MainClass

Can't produce a viable .jar with IntelliJ

First of all I'm not able to produce a .jar with IntelliJ without using Artifacts and I don't understand why. I make my project, build it, run it, make module but none of that produce de .jar in out directory... Maybe the reason is that I never wait to then end of the run and always interrupt with alt+F2 because the program is very long to run entirely, it's a bot.
So then I try to produce .jar using Artifact function in IntelliJ but it produces a .jar that I can't run :
-java prgr.jar returns Error: Could not find or load main class prgr.jar
and -java -jar prgr.jar returns no main manifest attribute, in prgr.jar
So I got few questions given that my program runs perfectly when I run the main class from IntelliJ :
What is the difference between commands : -java prgr.jar and -java
-jar prgr.jar
What is the MANIFEST.MF and how to use it ?
How can I do to produce a .jar that I can run from a shell ? How to use the artifact function properly, what files do I have to put and where in order to make it work ?
Edit : I just saw that I need the MANIFEST.MF but how do I tell the .jar where to find the MANIFEST ?
If you include any signed JARs in your app and then use IntelliJ to build artifacts, it will extract the JARS and bundle them with your compiled output.
This then causes a JAVA security exception. I've seen this with Eclipse Paho and Bouncy Castle which are signed.
You can check if any of the library JARs you are using are signed using the jarsigner tool.
jarsigner -verify -verbose <path to library JAR>
Change your IntelliJ artifact setup so that these get bundled as libraries instead of being extracted. Extraction invalidates the certificate as you'd expect.
Try creating a dummy project with just Main. Add 1 library JAR (that you are trying to build with) at a time. Build an output JAR each time until Main breaks. That's how I found this.
IntelliJ should warn you.....
I was finally able to solve my problem thank to this thread Wrong Manifest.mf in IntelliJ IDEA created .jar the most upvoted answer. I think it's a bug in IntelliJ that needs to be fixed : when the MANIFEST.MF is in main/java instead of /main/ressources it isn't included in the final .jar which mean that the .jar only contains the manifest relative to the external libraries but not the manifest that tells what is the main class.

Can't find or load main class JarSplice

Hello fellow programmers! I am writing a game at the moment, that uses the following libraries: LWJGL, OpenGL, Slick. I know the problems with simply exporting a JAR file from eclipse when using libraries like that, and I do use the JarSplice program to make a fat JAR. This has worked fine for about a month now, however, just recently it stopped working. Whenever I export the JAR from eclipse, and make it a fat JAR using JarSplice, I get this error:
Could not find or load main class Game.jar
META-INF (Located in the fat JAR):
Manifest-Version: 1.0
Launcher-VM-Args:
Launcher-Main-Class: cubix.main.Cubix
Main-Class: org.ninjacave.jarsplice.JarSpliceLauncher
Please note that the main method of the program is located in the Cubix.class, which is located in cubix.main.Cubix'
I have never came across this error before, and it is driving me insane. If any additional information is required, please feel free to ask.
EDIT:
I might also add that if I export any other project this way (using the same libraries and using jarsplice to build a fat JAR) it works just fine. I am only experiencing this problem with this specific project in eclipse. However, when I run Cubix in eclipse debugger, it runs just fine.
Try couple of things :1 check the content of the file, use jar -tf see everything is present2. can you try running this file from command/terminal.3 if all above is good, compile Cubix.java from terminal and place this .class file in jar file manually and check.
I'm sorry for posting this. The problem wasn't that it couldn't find the main class but rather I had typed the command line wrong. I typed: 'java Cubix.jar -jar' rather than 'java -jar Cubix.jar'. Also, after fixing the command line, it gave me the stack trace for the error that was overlooked by eclipse, and that one of the textures that needed to be loaded was case sensitive.

Java jar running

Any idea why
java -jar jarname.jar
works, but double clicking the jar gives a
"Could not find the main class:"
error? Manifest is correctly placed and double clicking was working until recently, but all of a sudden it gives that error. I'm not sure what changes I made though.
EDIT:
The command I used to create the jar was (in cmd):
jar cfe jarname.jar files.Main *
Main is part of the package files.
Add this entry in your manifest file:
Main-Class: com.abc.def.YourMainClass
Hope this'll help!
Some time ago I had a comparable problem. And it was caused by multiple JDK/JRE versions on my machine.
What is used by double-clicking differs from what is used by typing jar in a shell.
I think JarFix could help. Or you lookup what is associated with double-click in the file-associations (reinstall java might also help)
It seems to have been some weird compiler version problem. When I complied the class files using an older version of java, then compiled it into a .jar file, it was able to run.

Exporting an Eclipse Project into a Runnable Jar does not work

I'm trying to export my java application to a Runnable Jar file.
This worked before, but apparently I've tinkered with something and now it doesn't work.
Exporting from Eclipse works without problems, but when I'm starting the .jar an error shows up:
"Could not find the main class: game.Main. Program will exit."
Now, the game.Main class is my main class so that's ok.
When I run the .jar via command line like this:
java -cp D:\PathToMyJar\test.jar game.Main
It also works just fine.
I just can't run the .jar file itself.
The Run Configuration with which I'm exporting is set to the correct Project and main class.
I hardly used Eclipse's export features before, so I'm kinda out of options I could think of.
From what I've read it might be some problem with the manifest file, but I don't know where to check it or how to change it - apart from the fact that Eclipse should be able to do that on it's own somehow ...
PS: The manifest inside the jar file looks like this:
Manifest-Version: 1.0
Class-Path: .
Main-Class: game.Main
Main-Class is correct. I don't know what Class-Path generally is, but I guess that means wildcard, so OK too.
Make sure the jar contains manifest file with correctly specified main class.
Follow these instructions: http://www.skylit.com/javamethods/faqs/createjar.html

Categories

Resources