Creating .jar files (preferably in Eclipse) and adding existing .jar files to them
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
I have an application I would like to export as a .jar. I know how to export a .jar file using
project->
export->
export as jar->
etc.
. . . but how do you add existing .jar files to it? I have imported them into the Eclipse classpath, and the app works on Eclipse. But when I try exporting it as a .jar it starts and doesn't finish. On the command line I get a NoClassDefFoundError. This might be because text file I need has got out of place, or it is more likely that I haven't got a jar (called java_cup) in my output jar.
How can I add this pre-existing jar to my output jar? I would prefer to use Eclipse.
I expect the background behind this question is that you want to create an executable jar file. In that case the way to "add" another jar file to your executable jar file is not to put it inside the executable jar file, but to mention it in a Class-Path entry in your executable jar's manifest.
I don't think Eclipse will generate the manifest for you, at least not to this level of detail (but I could be wrong). So it's up to you to create the manifest file yourself and identify the main class and the other jars you want in the class path. It's also up to you to put those other jars in the right place relative to your executable jar, before you run it at the command line.
Putting a couple of jars in a folder isn't that onerous, is it? I've seen this question numerous times but I have never understood why it's asked. Maybe you could enlighten me.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
No, it isn't hard at all. In fact that is what I have got and it works nicely. But I can't double-click my jar to run it, and it would look nicer with a single file . . .
If double-clicking the jar file doesn't run it properly, then (assuming you're on Windows) your file associations for the .jar extension aren't set up to do "java -jar X.jar". Or if it is set up that way, then something else in your configuration isn't set up right. Distributing only one jar file isn't going to help that problem.
Almost every piece of software I've installed for the last several years has come via an installer. Nowadays if you send out some files and say "Put these files in a directory" then you look like an amateur anyway; whether you send one file or five doesn't make much difference. The only exceptions are programs written by programmers for programmers, and there I don't think the number of files matters either.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
Thank you again. This file is "written by programmers for programmers." So the number of files doesn't matter.