Eclipse export libraries into jar - java

So my old jar has the libraries exported into it as folders containing the .class files. And my new jar has each library .jar packaged into my jar in a libs folder like my workspace. I need it to export with the former setup. Pictures below explain my issue further.
The reason the packaging changed is because I reinstalled my OS and I don't remember how I had done it before since it's been a while since I last exported a jar for this project, I think what I did last time to get around this issue was I used IntelliJ Idea to export it but I'd much prefer figure this out with Eclipse. There seems to be a setting that does exactly what I want under the Runnable Jar Export wizard but I'm not exporting a runnable jar as it doesn't have a Main method.
old jar picture:
new jar picture:
new jar picture 2:

Yes, a fat JAR can be created via the Runnable JAR file, but not via the JAR file export wizard.
So either
create a dummy main method or
use Ant, Maven, Gradle or something else
to create a fat JAR.
Make sure that you put everything in the source folder that you want to copy to the output folder and include in the JAR.

Switching to IntelliJ Idea allowed me to export in this manner out of the box with default settings. If anyone knows how to get this function in Eclipse feel free to add it.
Edit: as per #howlger's comments I've found a solution where Eclipse works perfectly for my use case.

Related

Using Eclipse "export as Runnable Jar" without configurations

There should be something wrong in the way I'm using Eclipse, so let me explain:
I have a folder in the project marked as src, in order to have my configuration being deployed correctly in the classpath when Eclipse compile my project. This works fine since in debugging/running from the IDE works just fine. But if I export the project as a runnable jar file, the config files are embedded in it, and as a result I can't allow the user change the configuration ( I have to remove manually the file from the jar, and put in the current dir in order to achieve the desired behavior). Is there some solution to have this automatic? I did something like this: exported the "Export as a runnable JAR" as an ANT file, then manually modified it to work as I want, but unfortunatelly I have to manually update the file when class path changes. Any suggestion?
In eclipse when you choose export option, you'll see JAR file, Javadoc and Runnable JAR file. Can you try JAR file option, where you can select/deselect the classpath along with the project? It also has an option to select only the class files without resources. Hope that option will be useful.

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.

How to test .jar versions?

I have a project I'm working on and planning to export as a .jar to be released to public. However a major problem I have with Java is that some things work in Eclipse, but won't as a .jar file (ie directory problems: sometimes the directory is right in Eclipse, but when exported it will no longer work). I would like to know if there is a way to check for errors in a jar file directly in Eclipse. I know you can import the new jar file as an external jar file and run it that way but I don't like having to reimport a new jar file every single time a create a new version of one. What can I do?
You can write logs in your code and see that logs in java console when your jar start executing.
how exactly you are building jar ? using command or anything else.?

Jar file not finding libraries

So I have a small Java program with some gui that I have runnning in eclipse just fine. I followed the fatjar tutorial to create the .jar, but when I try to run the .jar the first gui window appears but none of the functions work. When I ran it in cp, clicking the button generated an exception basically saying some of the object types could not be resolved as type, all of which were from import libraries. Has anyone seen this before, I tried using this program jarfix for an issue similar to mine, but nothing.
Please define
When I ran it in cp
The format of the java command should look something like
java -cp /path/to/jars com.main.class
You could start locating the source of the problem by opening the jar file with an archive program (e.g. winzip) and look if the class / type is included in the jar file or not.
If it is there it might be a classpath issue. If it is not there something is wrong with building the jar file. Musn't the included jar file be set in the "Order and Export" tab in the java buildpath dialog?
I am not aware of fatjar. But I faced similar problem, might be somewhere it is linked to your issue.
In my case everything was working when I was running through eclipse, but when I created JAR using Eclipse I faced issue as what you are facing, but in my case it was issue of accessing resources inside JAR File.
So solution was,
Right Click -> Export as -> Jar, here check the box "Add Directory Entries" and then create the JAR. Ans all worked.
Above all, first you should check whether the jar which is generate is correct or not by just open it with winrar or similar tool and see all classes and resources are placed properly.
An alternative would to place all those jar files in the same folder. i.e. if your application jar file is App.jar and other dependencies are A.jar and B.jar. Then drop A.jar and B.jar in the same folder as App.jar.
If you double click on App.jar it will by taking the other dependencies in the same folder by default.

how do I add classpath to the exported runnable java .jar file using eclipse

so my projects have a lot of classpath variables set in the run configuration -> classpath tab in eclipse...
but once I exported the project as a runnable java jar file, none of these classpaths are exported to the jar's manifest file despite having specified this run configuration during the export dialogue....
how do I get eclipse to add these classpath variables to the exported jar's manifest?
besides doing everything manually of course...
You can create your own manifest file and ask eclipse to use it in the "Export->Jar" wizard.
You can also try "Export -> Runnable Jar" in the latest versions of eclipse. I have not tried it, this generally is supposed to include the referenced libraries, so it might include the classpath variables as well.
Eclipse doesn't seem to have an option for creating a manifest, I think the export option is for library files rather than runnable jars. If I remember correctly you have to manually sign+add a manifest to a jar at once? Not sure though. But you would have to manually sign it anyway so not a massive deal.
Check out this post - i believe it answers your question.
Java: export to an .jar file in eclipse

Categories

Resources