Include external library jars in jar to mac .app conversion - java

I am sorry if this question is placed wrong, but considering it is about jars and Java I figured it would probably be the closest to my subject.
I have a Java program of which I have made, this program I am going to distribute for Mac and Windows. To do this for Windows I have used Launch4j, which works without any issues. Now the problem is that when I try to convert the jar into .app for Mac, it doest work, the app just opens and then closes right after. I suppose this is because when converting to Mac .app using Appmaker it doesn't include the external libraries I have used for the program. Because of this I am now wondering how I would add the external libraries to the .app file like I have done on the .exe, where I have just added the .lib folder with all the external library jars into the folder where the .exe is in.
I have tried adding the .lib folder to the content folder which is created for the .app file, but that doesn't work, I have also tried adding it to the ressource folder within the .app file.
One of the options I have found myself is to combine all external libraries and the actual program .jar file into one. It just seems very difficult and really I don't need it to be just one combined .jar, if it can be included in another way.

Not sure of Appmaker but can do something like an uber jar, i.e. a single jar that contains all the classes, basically take all the jars unzip them to a signle folder and then jar them up again.
Not sure what you are using to build but maven has a maven shade plugin that does exactly this.

Related

Ship and call a .exe from within a .jar [duplicate]

I have a huge JAR file, which I created with Maven Shade plugin.
I run this with java -jar foo.jar , and my UI opens. Now I want to execute *.exe file, which is also in that JAR file, how can I do this?
I tried putting the exe to my classpath and run it from there, but after trying I found out that classpath is actually the location, where my JAR is.
Any suggestions?
Found this thing here, but is this really the best solution? Seems like alot of work, I think I have different case here, because I can define the location of exe myself and the JAR is created by me.
run exe which is packaged inside jar
Why I need this?
I want to give user a single file executable, which he can run, but my program is using the *.exe. Should I put the exe next to my jar and there will be 2 files or there is solution for my requirements?
Copying the file to a temporary location and running it is the way to go. The answer you linked to does much more work that necessary, as you can get your exe file as an InputStream and copy it to a file with a utility like Apache Commons IO FileUtils.copy(in, out)
See How do I copy a text file from a jar into a file outside of the jar? for example.
It's not about the location, it's about the fact that you need to tell your OS to run the exe and, unfortunately, you can't do that by providing a location within a jar.

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.

Creating a .exe for a java project

I've developped a game during a Game Jam and I'd like to create an executable to distribute it to the other team members.
The game uses the slick2d and lwjgl library. I've tried to use JExePack, but the .exe file I get isn't runnable, I get an error while launching it.
Even the jar file gets me errors.
I'm only able to launch the game on the IDE. When I launch it with the command line : java -jar "game.jar", it obviously tells me that there's missing libraries, even if I indicate the path to the lib folder.
Is there an easy way to create an executable ?
Thanks in advance.
I think using a jar was a good idea.
You need to add every required jar in the classpath one-by-one for the jar to run properly.
Launch4J (http://launch4j.sourceforge.net/) is my favorite tool for that. You can just export an executable jar from your IDE and create an exe out of it. Creating an executable jar in Eclipse gives you the option to include all required libraries in it, which saves you from adding them manually when create the exe file. The minimal settings you need are:
Input (your executable jar)
Output (the .exe you want to create)
Minimum Java version (i.e. 1.6.0)
Thats it (as far as I remember)
If you want the exe you can use exe4j, it's a very useful tool, but i think using jars is better since you can run them on every platform. Anyway, when you export the jar, check on your ide's preferences if it automatically imports the libs. (for example, on eclipse you can pack the required libs into the exported jar)
You can use Luncher4j to create an exe file and convert the jar libraries to dll files.

Classpath and portability

I am new to Java and am using Eclipse to write Java code.
I've added lots of library (.jar) files as referenced libraries. I've also exported my project as a JAR file. My question is if I run this file on a computer where the referenced libraries are not at the same place as in my computer, will it run successfully?
I also made a runnable JAR file, whose size was much larger (~29 MB) as compared to the previous file (~24 KB).
My question is if I run this file on a computer where the referenced libraries are not at the same place as in my computer, will it run successfully?
No. You need to package the referenced JARs alongside your program JAR. It would be inconvenient for the end-user to have to download all the libraries that your program depends on. A couple of options, both possible using the Eclipse Export function.
Unpack all your dependent JARs and package them together with your code into one single executable JAR.
Specify in your program JAR manifest the classpath, which will contain relative paths to the location of your dependent JARs. These could be for example in a lib folder. The location of the lib folder relative to your program JAR would need to be the same for all your end-users, so it would make sense to package it all together.
It depends on the way you package jars with the runnable jar app. if you do it inside the jar or outside it then you should make a classpath entry to your manifest.mf.
you can even bundle the reference library in the same jar. So as you reduced the size to that extent i assume you removed those from the jar, which is not good approach in most cases. It is not recommended not to include referenced jar, unless you are 100% (not even 99.9%) sure those library WILL be in the system you would want to run.
EDIT:
to include the referenced jar in eclipse, you need to goto
project-properties>java-build-path>order-and-export
here check whatever libraries you want.

My Java Slick game Jar file will not open

I have made a simple GUI using slick and lwjgl, I am using eclipse and have imported my jars, natives and images to my Java build path, I export my code as a jar file and get a jar file on my desktop, I try open it with SE Binary and it loads for a little while then nothing happens.
It may have something to do with how I have imported them into my build path, I will try explain, I have inside my Java project two additional folders that I have created, they are lib and res, res holds all of my images and lib holds two other folders called jars and natives, jars holds two jar files (slick and lwjgl) and natives holds 4 other folders (windows, linux, mac, solaris natives), I imported my 2 jar files into the build path using the add jars option, I then imported my res file using the add class folder option and after that using the same option I imported the lib folder, I then exported it and now it doesn't open.
It may be worth mentioning that before I export my jar files from eclipse I get the following pop up message: The operation repacks referenced libraries.
It seems that lwjgl can't use the native libraries when they are inside a jar archive.
The solution is to unpack your natives folder either into some install directory or a temporary directory. Then, before you use any lwjgl function, you need to set the new path to the natives. For example with the files unpacked in "lib/native" I use
System.setProperty("org.lwjgl.librarypath", (new File("lib/native")).getAbsolutePath());
System.setProperty("java.librarypath", (new File("lib/native")).getAbsolutePath());
System.setProperty("net.java.games.input.librarypath", (new File("lib/native")).getAbsolutePath());
This seems to be the easiest solution. For example minecraft (which uses lwjgl) unpacks the natives to "%appdata%.minecraft\bin\natives" (on windows, ~ instead of %appdata% on linux).
Edit: a very related wiki page: http://lwjgl.com/wiki/index.php?title=Distributing_Your_LWJGL_Application

Categories

Resources