• Create Account

# [java] Exporting jar.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

15 replies to this topic

### #1Tankyroo  Members   -  Reputation: 102

Like
0Likes
Like

Posted 28 November 2011 - 02:40 PM

Hey everyone, I'm having trouble exporting my first game as a .jar file, Here are the steps I'm using:

(I'm using eclipse)
Right-click on my project, click export.
Select type "JAR File".
Select my whole project, making sure all necessary files are selected.
Generate the manifest, select my main class as the main class.
Click finish, finishes exporting without any errors
Double click on the file that was just exported, and it gives me the error: "Could not find the main class: slickGame.src.MainClass. Program will exit."
Right click on .jar, open with WinRAR, go to META-INF, edit the manifest, everything is in order.
Contents of the manifest:
"Manifest-Version: 1.0
Main-Class: slickGame.src.MainClass

" (Yes there is a newline after.)

I'm not sure what is going wrong, I've tried searching but everything just tells me to edit the manifest which is already correct.

Not sure if this matters, but I'm using the Slick2D Library (hence the name slickGame.)
I'm also packaging the files for those to work. (dlls, slick.jar, and lwjgl.jar)

Any help is appreciated, thanks in advance.

### #2Angex  Members   -  Reputation: 743

Like
0Likes
Like

Posted 28 November 2011 - 03:53 PM

Have you verified the class files have been exported and put into the correct structure?

From the root of the jar, is there a "slickGame.src.MainClass.class"?

### #3Tankyroo  Members   -  Reputation: 102

Like
0Likes
Like

Posted 28 November 2011 - 03:56 PM

Have you verified the class files have been exported and put into the correct structure?

From the root of the jar, is there a "slickGame.src.MainClass.class"?

I open up the jar, and I see multiple files/folders, I open the "slickGame" folder, then I open the "src" folder and in there I see MainClass.class, if that's what you mean.

### #4Glass_Knife  Moderators   -  Reputation: 1822

Like
0Likes
Like

Posted 28 November 2011 - 06:48 PM

You should try running your program from the command line without the jar file. Make sure that works first.
If you put the /src folder in the jar file, the manifest won't find the class files, because it doesn't look in the "src" folder.

You could add the src folder to the classpath, but you haven't done that.
You would have the same problem if you opened a command window at the project and
tried to run the program.

Are you compiling by hand? Normally, with the default Eclipse project, the *.class files end up in the /bin folder.
If you didn't set it up to put the *.class files there, are you compiling with the command line?

Either way, if the only folder in your jar file is the slickGame package folder, it should work.
I think, therefore I am. I think? - "George Carlin"

### #5Tankyroo  Members   -  Reputation: 102

Like
0Likes
Like

Posted 29 November 2011 - 05:53 PM

You should try running your program from the command line without the jar file. Make sure that works first.
If you put the /src folder in the jar file, the manifest won't find the class files, because it doesn't look in the "src" folder.

You could add the src folder to the classpath, but you haven't done that.
You would have the same problem if you opened a command window at the project and
tried to run the program.

Are you compiling by hand? Normally, with the default Eclipse project, the *.class files end up in the /bin folder.
If you didn't set it up to put the *.class files there, are you compiling with the command line?

Either way, if the only folder in your jar file is the slickGame package folder, it should work.

First off, thanks for the reply.
Secondly, I'm still pretty new to java, so I don't know how to compile with the command line (or even where to find the command line to be perfectly honest.)

Edit: After a little bit, I figured out how to compile and everything with the command line, but while compiling I get errors from what I think is not having the slick2d library in the right place. I'll keep messing with it and report back soon.

Edit2: I still can't figure it out, I've spent a good amount of time on it, I'm going to take a break, maybe someone will reply with some more help by then.

Edit3: I don't know what's wrong, I followed a tutorial just to make a simple jar, and that doesn't work for me either, here are my steps:

contents of hello.java:
import javax.swing.JOptionPane;

public class hello{

public static void main(String[] args){
int fr = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null, "This is the body of the Executable's\rMessage Dialog.","Hello", fr);
System.exit(0);
}

}
Opened command prompt
changed directory to where it is located (C:\j)
> javac hello.java
> java hello

Exception in thread "main" java.lang.NoClassDefFoundError: hello
Caused by: java.lang.ClassNotFoundException: hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
Could not find the main class: hello.  Program will exit.


Anyone know why?

### #6Kyan  Members   -  Reputation: 395

Like
1Likes
Like

Posted 29 November 2011 - 09:54 PM

Anyone know why?

First, one of the nice things about Java (and .Net) style languages is that their errors are designed to be human readable. Thus, when you get an error such as java.lang.NoClassDefFoundError you can generally copy + paste into Google and find something.

In this case, it looks like your classpath isn't set correctly. Take a look here for some possible solutions. The easiest would probably be to modify your command slightly to:

> java -cp . hello

Assuming that your hello.class file isn't in another directory. To permanently correct the issue (as by-hand solutions will fade when you reboot) you'll need to set the CLASSPATH variable to something sane using Windows' ENV-VAR dialog. Other culprits can be incorrectly setting JAVA_HOME although that usually manifests in being unable to find javac. Also, it always helps to do a dir (or ls) after you compile to ensure Java generated your class files correctly (and in the correct place).

Once you've verified that java works correctly, make a test jar:

> [text editor] hello.mf
Main-Class: hello

> jar cvfm hello.jar hello.mf *.class
> java -jar hello.jar

Edit: After a little bit, I figured out how to compile and everything with the command line, but while compiling I get errors from what I think is not having the slick2d library in the right place. I'll keep messing with it and report back soon.

Sounds like you need to manually set both the class and library paths so that Java can find your dependencies. The classpath is so that Java may find your .class files (including those packaged in a jar) while the library path is for specifying native libraries to load at runtime. So you would do something like:

> javac -classpath [path to required jars] [arguments]
> java -Djava.library.path=[path to native libraries] [arguments]

Please note that if Java failed to compile correctly (for whatever reason) then you won't have a valid "main" class file. Thus, you will also get a java.lang.NoClassDefFoundError if you attempt to run it from a jar.

For what it's worth, using a tool like Maven can make this much less aggravating.

### #7Tankyroo  Members   -  Reputation: 102

Like
0Likes
Like

Posted 29 November 2011 - 11:44 PM

Well I finally got the tutorial example to work (not completely sure how) and I'm still running into problems with my game, I tried doing it the same way as the one that just worked, but I get 47 errors, most of them are like this:

cannot find symbol

symbol  : variable Keyboard

location: class src.Platformer // (Trying to compile another file to see if that will work, that's why the name isn't slickGame like in the original post.)

if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))

^

method does not override or implement a method from a supertype

### #8Tankyroo  Members   -  Reputation: 102

Like
0Likes
Like

Posted 01 December 2011 - 01:07 PM

I'm not sure what the rules say about bumping, but I still can't figure this out.

### #9m3rlino  Members   -  Reputation: 202

Like
0Likes
Like

Posted 01 December 2011 - 01:33 PM

I'm not sure what the rules say about bumping, but I still can't figure this out.

Hello. I think there are some problems in the Keyboard class in the library. did you compile it with Eclipse? If so you click with right button of the mouse on the project. Click properties. Then look for java build path...
There should be everything you need about how to compile the program. Remember that you can run directly your application from eclipse without having to compile from the command line. Everything it should be automatic and should solve all your problems and lack of times about configurations.
It may be that you lack some library or maybe they change the protection of some constants, maybe before they were public and now they are private and they should be accessed with getCONSTANT getter method.

Remember about assembly dependencies too in the properties window. There you can specify what you want to export and in which directory.
I hope I can help
Evil bay - Our new game at FourLeafGames! (Link to the game)

### #10Tankyroo  Members   -  Reputation: 102

Like
0Likes
Like

Posted 01 December 2011 - 03:28 PM

I'm not sure what the rules say about bumping, but I still can't figure this out.

Hello. I think there are some problems in the Keyboard class in the library. did you compile it with Eclipse? If so you click with right button of the mouse on the project. Click properties. Then look for java build path...
There should be everything you need about how to compile the program. Remember that you can run directly your application from eclipse without having to compile from the command line. Everything it should be automatic and should solve all your problems and lack of times about configurations.
It may be that you lack some library or maybe they change the protection of some constants, maybe before they were public and now they are private and they should be accessed with getCONSTANT getter method.

Remember about assembly dependencies too in the properties window. There you can specify what you want to export and in which directory.
I hope I can help

I tried what you said, but I couldn't find anything that looked wrong.

What I did was add this to my manifest:
Class-Path: lib/lwjgl.jar lib/slick.jar
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?

Edit: I didn't only add that Class-Path code to the manifest, I also put the required files outside of the jar in the same folder, but I'm still having that problem where it opens and closes.

### #11Tankyroo  Members   -  Reputation: 102

Like
0Likes
Like

Posted 03 December 2011 - 02:00 AM

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

### #12Kyan  Members   -  Reputation: 395

Like
0Likes
Like

Posted 03 December 2011 - 02:45 AM

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?

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:

• 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.

### #13Tankyroo  Members   -  Reputation: 102

Like
0Likes
Like

Posted 03 December 2011 - 11:35 AM

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?

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:

• 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.

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.

### #14Glass_Knife  Moderators   -  Reputation: 1822

Like
0Likes
Like

Posted 05 December 2012 - 04:41 PM

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"

### #15Bubsy  Members   -  Reputation: 407

Like
0Likes
Like

Posted 06 December 2012 - 06:35 AM

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

### #16dmatter  Crossbones+   -  Reputation: 2417

Like
0Likes
Like

Posted 06 December 2012 - 03:41 PM

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.

Edited by dmatter, 06 December 2012 - 03:42 PM.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS