Can't produce a viable .jar with IntelliJ - java

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.

Related

Building .jar file with IntelliJ IDEA gives me a file that can't be used

I'm currently working on a project where I'm using the LWJGL library and I just tried building a .jar out of it all, now the problem is that nothing happens when I try to start the .jar.
My only thought is that maybe the library didn't export correctly but I've been looking around at the other questions here for a while and I've have added everything correctly at the Dependencies tab in the Project Structure, the libraries I need are also in the Artifact Output Layout as extracted directories.
I just tried checking the stacktrace with cmd and it says that it can't find lwjgl in the library path so I'm guessing there's something I don't know about .jar files or something that's causing this error.
Here's the stacktrace:
Also in IntelliJ I've put "-Djava.library.path=lib\native" in VM options, is it possible that this doesn't apply to the .jar or something.
Help is appreciated!
There is a rather complicated process to make an executable jar with LWJGL. You need to use Jarsplice.
Jarsplice has 4 steps which are pretty self explanatory but here they are anyway:
1: Add Jars.
Add the jar you generated as well as all the libraries. That includes lwjgl.jar, and any other jars you used.
2: Add Natives.
Add all the natives you used in your project. These are the files you referenced using -Djava.library.path.
3: Main Class
Select the main class of your program. This could be something like com.example.game.EntryPoint.
4: Create Fat Jar
Click "Create Fat Jar" to create your executable jar!
You can also optionally create a Windows .exe, OSX .app, and Linux .sh executables as well.

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 Program only runs in IDE, not exported as a Jar

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 ?

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

Unable to run JAR file on another PC. Possible issue with Environment Variables?

I've built a JAR file and it executes fine on my PC (XP) which has Eclipse installed. It also works on another PC, which also has Eclipse.
I've tried running it on another PC(XP) that does not have Eclipse. Though it contains the JDK and multiple JRE. The JAR file just does not execute by clicking or from the command prompt.
I am not entirely sure, but my best guess is the Environment Variables are not set properly. Here is the error I receive from the command prompt:
Exception in thread "main" java.lang.NoClassDefFoundError: ...
Any help would be appreciated.
It must be a CLASSPATH issue.
The stacktrace should also say which class it failed to find. Once you have that, then find which jar has that class. Then add that jar file to your classpath or add it to the classpath env variable.
This is likely a classpath issue as others have said.
One thing to note is how your jar is constructed. You have a number of options in the dialog for exporting a runnable jar;
Extract classes into jar
Zip dependencies into the jar - creates jar-in-jar-loader.jar inside the jar.
Place jars in a subdirectory next to the jar.
Depending on what you have chosen for this depends on how the jar will behave. If the classes are extracted, dependent classes not in the JDK should be on the classpath. I'd recommend this course of action as it is simpler.
Now, the question is - are you using a dependency on your classpath not in the build dependencies of the eclipse project? If so, it won't be packed with / zipped into / put next to the jar because eclipse doesn't know about it (but java will still find it on your system because it's on the classpath). Also, if you've saved an ANT script and updated the build path in eclipse, eclipse won't update that ANT script - that is generated once only.
Environment variables are not considered when invoking a jar file when clicking on it (equivalent to running javaw -jar your.jar).
I'm pretty sure that it doesn't work on your first PC outside of Eclipse either.

Categories

Resources