My Java Slick game Jar file will not open - java

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

Related

JavaFX SDK 19 contain no .dll Files to create JAR with external libraries

in previous versions of JavaFX, you could build jar files from project by building artifacts using .dll files within the bin folder of JavaFX. However, in later versions of JavaFX, there are absolutely no bin files or .dll files. Does anyone know a work around this?
This is not true. If you, e.g., download JavaFX from Gluon and unpack the distribution file all the .dylibs, .dlls, etc. are contained in the lib/ folder. (Just verified with openjfx-20-ea+11_osx-x64_bin-sdk.zip)
The jars in the maven repository are structured a little bit differently. The have platform specific variant, e.g., javafx-graphics-20-ea+11-mac.jar which contain the binary libs at the top-level folder.
Going modular, as #jewelsea suggested above, is yet another option, depending on what you want to achieve.

How to include resources in a Java game on MacOS?

I have written a game with external resource files (images, sounds, etc..) in Java, which works on Windows when I export the .jar to .exe but not on MacOS when I export the .jar to .app .
I wrote the game in the Eclipse IDE and exported it as a runnable .jar file from there too.
My process for making it run on Windows is I first used an application called "Launch4j" to export from .jar to .exe . Then I made a copy of the folder I developed the game in so that all of the resource files are organized in the right way. Finally, I just put the .exe file in the main directory and it works.
My process for trying to make it run on MacOS was I first used an application called "AppMaker" on a Mac to export from .jar to .app . Then, I made a copy of the folder with the resources and put the .app file in, but it didn't open.
I noticed that if I exported a game without resources to .app and put it in the "Applications" folder on Mac, it worked, so I tried that with the resource game by putting my resource folders inside the actual Applications folder, but it also didn't open.
The only way I actually got a resource Java game to work on Mac was by not exporting it to a .app file at all and just leaving it as a .jar in the folder with the resources, but I really like how you can change the .app icon and it actually shows the file as an application.
Is there any way I can get my .app file with resources to run on MacOS?
I think that a FatJar might help you.
A Fat Jar is an archive which contains both classes and dependencies needed to run an application, here you have some info about it: http://tutorials.jenkov.com/maven/maven-build-fat-jar.html

Include external library jars in jar to mac .app conversion

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.

Creating an executable jar using Eclipse

It export it fine as a Jar, but when I double click the project it won't load up.
I'm building a Java Slick2d game (my first). I've tried on both windows 7, and Ubuntu 12.10. Any suggestions?
If you run the exported jar file from the terminal, you'll see an Unsatisfied Link Error. To resolve this, you need to include the LWJGL natives. If you want everything in a self-contained jar, the easiest way is by using JarSplice.
Let's walk through how to do this step-by-step.
Open up JarSplice
Add the jar you exported from Eclipse.
Add the LWJGL natives. These can be downloaded from http://www.lwjgl.org/download.php. I'll be exporting for Linux, so I'm going to include the Linux native files. The native files should be in a folder called native.
Now specify the main class in your project. Don't forget to include the class's package.
Click Create Fat Jar and you're done! You can also export to a Linux .sh, Mac .app, or Windows .exe. Just make sure you include the appropriate natives.
Have fun sharing your game with others!
make sure you mention the Main Class: in META-INF
Detailed instructions
The detailed instructions for creating an executable JAR file for a stand-alone SWT file are listed below.
Create a runtime folder for the desired runtime target on your system (e.g., c:\swt\runtime-linux). Note that the target platform does not need to be the same as your development platform.
Find the correct SWT JAR file for the desired target platform. You can download the desired ZIP file from the SWT website. For example, for Eclipse 3.3 and a target platform of Linux, download the file swt-3.3.1.1-gtk-linux-x86.zip. Expand this ZIP file and copy the swt.jar file to the runtime folder. Remember that this swt.jar file is specific to one platform, in this case Linux.
3.Create a manifest file for your application using the Eclipse text editor (e.g., myapplication-manifest.txt). The text of the manifest should be as follows:
Manifest-Version: 1.0
Class-Path: swt.jar
Main-Class: mypackage.MyClassWithMainMethod
(blank line at end of file)
4.Make sure the manifest file ends with a blank line. Put the name of your package and class that contains the main() method for the Main-Class.
In Eclipse, select File/Export/Java/Jar file and press Next.
On the JAR File Specification dialog, select the source files for the classes you want in the application. In the export destination, browse to the runtime folder and enter in the desired name of the JAR file (e.g., myapplication.jar or myapplication_linux.jar). Press Next.
On the JAR Packaging Options dialog, make sure the "Export class files with compile warnings" box is checked. Otherwise, if your source files have any compile warnings, they will not be included in the JAR file. Press Next.
In the JAR Export dialog, select the option "Use existing manifest from workspace". Browse to the manifest file you created above. Press Finish.
If the JAR file already exists, you will be asked to overwrite it. Select Yes. If your project had any compile warnings, a message will display. If so, press OK.
At this point, the JAR file for your application has been created in the runtime directory.
If needed (i.e., your target platform is different than your development platform), copy the runtime directory to a directory on your target platform.
In your operating system's file explorer, browse to the runtime directory and run your JAR file. For example, in Windows, you can just double-click on it in the Windows File Explorer or, from the "cmd" prompt, you can enter the command: java -jar myapplication.jar. The application should run.
Find file "MANIFEST" in the jar file, and add line :
"Main-Class: {Your executable class name (the class with main method)}"

Adding comm.jar into my jar

I add comm.jar library into my .jar and I get javax.comm.NoSuchPortException.
Is this bug because I haven't add javax.comm.properties and win32com.dll into my jar? How can I add this file inside manifest?
Java does not support Jars within Jars. The correct way to ensure the javax.comm packages are on the run-time class-path of the application is to add a manifest that provides a relative path to the dependent Jar, and ensure that Jar is in the right place to be found. For details see Working with Manifest Files: The Basics & particularly Adding Classes to the JAR File's Classpath in the Java Tutorial.
There are other methods that can be used for applets and apps. launched using Java Web Start.
#Gogoo's answer says to copy stuff into your JVM installation directory.
It should work, but it is generally not a good idea:
The stuff that you install that way will be shared by all applications that you run using that installation. Depending on the what it is, it may interfere with other applications.
Each time you update your JVM, you have to remember to copy those files into the new installation directory.
IMO, a better idea is to put those files in a separate directory tree, and write a wrapper script to launch your application with the files on the classpath and library path. And/or see #Andrew Thompson's answer.
Try FatJar.
The Fat Jar Eclipse Plug-In is a Deployment-Tool which deploys an
Eclipse java-project into one executable jar.
It adds the Entry "Build Fat-JAR" to the Export-Wizard. In addition to
the eclipse standard jar-exporter referenced classes and jars are
included to the "Fat-Jar", so the resulting jar contains all needed
classes and can be executed directly with "java -jar", no classpath
has to be set, no additional jars have to be deployed.
copy comm.jar \jdk1.6\jre\lib\ext
copy win32com.dll \jdk1.6\bin
copy javax.comm.properties \jdk1.6\jre\lib
and start your.jar over command line:
java -jar your.jar

Categories

Resources