[java] Exporting jar.

Started by
14 comments, last by dmatter 11 years, 4 months ago
Once again, sorry for the bump, but I'm really stuck on this.
Advertisement
Once again, sorry for the bump, but I'm really stuck on this.[/quote]
Relax, it's fine.

And that fixed my previous error, but now what happens is when I try to open the jar file, the game window opens but closes immediately (Progress!). Any insight as to why?[/quote]
First, what is it you are actually doing? Is this a simple "Hello, World!" but with your application requiring the other two as a test? We can help you better if you show us what it is you're trying to do - e..g what does the "main" portion of your application look like (in code)? That said, if you're certain your application isn't bailing immediately for some reason then don't worry about it.

Second, are there no errors? In this same vein, are you passing on any exceptions (e.g. just using empty catch(){} blocks) that might give an indication of a problem?

I'm not trying to be obtuse but I think there's some confusion regarding what you've actually done at this point. So, if I read this aright you've:

  • Compiled your application successfully.
  • Packaged your application into a .jar
  • Packaged your dependencies (LWJGL and Slick) into the jar as well.
  • Altered the manifest so that your application can find your dependencies correctly.
  • Tried to run the application and it just "starts then stops".
  • ??? output ???

One of the key steps missing is what you did with the native libraries (.dll, .so, or .dynlib depending on your platform) as those are required for your application to run correctly.

Once again, sorry for the bump, but I'm really stuck on this.

Relax, it's fine.

And that fixed my previous error, but now what happens is when I try to open the jar file, the game window opens but closes immediately (Progress!). Any insight as to why?[/quote]
First, what is it you are actually doing? Is this a simple "Hello, World!" but with your application requiring the other two as a test? We can help you better if you show us what it is you're trying to do - e..g what does the "main" portion of your application look like (in code)? That said, if you're certain your application isn't bailing immediately for some reason then don't worry about it.

Second, are there no errors? In this same vein, are you passing on any exceptions (e.g. just using empty catch(){} blocks) that might give an indication of a problem?

I'm not trying to be obtuse but I think there's some confusion regarding what you've actually done at this point. So, if I read this aright you've:

  • Compiled your application successfully.
  • Packaged your application into a .jar
  • Packaged your dependencies (LWJGL and Slick) into the jar as well.
  • Altered the manifest so that your application can find your dependencies correctly.
  • Tried to run the application and it just "starts then stops".
  • ??? output ???

One of the key steps missing is what you did with the native libraries (.dll, .so, or .dynlib depending on your platform) as those are required for your application to run correctly.
[/quote]
That is what I've done, you have read it right.
My dlls and everything are both in the jar in a folder called "lib" (just like they were in eclipse) and they are also outside of the jar with the same folder name just in case.

This is the "main" portion of my code (If this isn't what you mean, I can post other stuff.):
public static void main(String[] args) throws SlickException{
AppGameContainer app = new AppGameContainer(new Platformer());
app.setDisplayMode(800, 600, false);
app.start();

}


And yes, when I run the .jar the only thing that happens is the window opens, and then closes immediately, before anything loads, so the glimpse that I see of it is just a while screen, no title or anything either.

Also, about output, there are no errors or anything.

Edit: It looks like I finally got it to work!

Thank you everyone for your help!




for people who might have this same problem, here is what I did:
In command prompt I changed the directory to where everything is located.
I then entered:
>java -Djava.library.path=C:\j\lib -jar test.jar (C:\j\lib is where my library files are. lwjgl.jar, slick.jar, and all of the .dlls that go with it.)
And that opened my program successfully!
But, I didn't want to open command prompt to use my program every time.
So in the same directory as my jar and the necessary files, I made a batch file named run.bat.
The contents of the .bat are the same as I entered in command prompt. (java -Djava.library.path=C:\j\lib -jar test.jar)
Now all I have to do is open run.bat and it will run my game!
I hope this helps people who have a similar problem so they won't have to spend nearly five days trying to figure it out.
When doing some JNI work, I found it necessary to add the folder containing the *.dll files to the system PATH.
Once I had done that, (this was trying to access native libraries with TOMCAT), and after I rebooted the
computer everything worked.

I think if you add that folder with the *.dll files to the PATH, you won't need to do any magic with the script.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

You could also try to programmatically set the Java startup variables.
[source lang="java"]System.setProperty("java.library.path", "%String%");[/source]
P.S. I didn't test the code above

In command prompt I changed the directory to where everything is located.
I then entered:
>java -Djava.library.path=C:\j\lib -jar test.jar (C:\j\lib is where my library files are. lwjgl.jar, slick.jar, and all of the .dlls that go with it.)
And that opened my program successfully!

Ridiculously, you can't usually package dependant jars or native libraries within a jar. Nothing stops you putting them in there but the standard classloader won't inspect them. This is why it only works once you direct it to your external dependencies. The usual thing to do is simply to not even try and package them in jar. A good-looking solution that I've never tried would be to use a 3rd party classloader like http://www.jdotsoft.com/JarClassLoader.php, it also allows packaging of native lib too.

This topic is closed to new replies.

Advertisement