I'm still pretty new to java and I'm VERY new to exporting .jar files. I've got a little game that I want to send to some friends and I was told in another question that I should export it to an executable jar file. Well I finally got that working on my computer but when I send it to other people it doesn't work because they don't have the library.
I'm importing the objectdraw library and without that my program won't run at all!
So basically I need to find a way to export the object draw library as part of my .jar file so that they can use it too. Do I simply include it in the included files part of the jar command?
ex: jar cmf MANIFEST.mf Archery.jar * /System/Library/Java/Extensions/objectdraw.jar
or what? I'm working out of the command line right now.
The simplest way is to send the JAR library file too and add a Class-Path entry to the manifest. This entry would look like:
Class-Path: objectdraw.jar
You could also set the CLASSPATH environment variable manually.
Alternatively, you can unpack the library and add all (or just the required files) to your final jar. This doesn't always work though, because some libraries rely on the integrity of teir JAR file.
Finally, it is possible to include the dependency in the main JAR, but it would require a custom class loader.
Turns out the best way I've found to do this is to unpack the library and then put all the resulting files in with your final archive. This way it actually works on other computers.
jar xf library_wanted.jar; jar cvmf MANIFEST.mf end_result.jar *.class library_wanted/
Related
I have an issue that is really annoying right now.
For a school project (that is due on monday :( ), I have to submit a .JAR file that is a stand alone app and that includes sources.
However, in Eclipse, I didn't find how to export the sources and at the same time, include the required libraries.
My BuildPath is set up this way :
http://image.noelshack.com/fichiers/2014/19/1399746312-owp08.png
When I do :
Export as a runnable JAR file, everything works in my program but I don't have the sources inside the JAR
Export as a JAR File, I can add my sources, but when i try to run the JAR file, I have this exception :
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Here are my settings :
http://image.noelshack.com/fichiers/2014/19/1399746469-sans-titre.png
I really don't know what to do, I've been searching for hours now, can someone help me ?
Thank you in advance.
Regards,
Azsde.
Well, the short answer is that eclipse doesn't support doing that directly. This is why most people use other tools such as Ant or Maven in their projects, rather than relying on the IDE.
You can work around eclipse's lack of an export source option in the runnable jar export wizard in one of two ways:
You can create a runnable jar and then manually add your source files to it
You can create a normal jar which includes your source files and add a MANIFEST.MF file to it.
Adding source files manually
This is what I would normally consider a terrible option, but since this is a homework assignment, there are two mitigating factors:
this is a one-off
you are short on time
If you have neither the time nor inclination to learn the details about manifests, I would recommend this option.
Adding your own MANIFEST.MF
As you may have noticed, even when you select the option to have eclipse generate your manifest in the export wizard, your jar file won't actually run. If you open up the 'normal' jar file and look at the generated manifest file it will look something like this (for a simple HelloWorld program using one third party library):
Manifest-Version: 1.0
Main-Class: HelloWorld
Whereas if you open up the manifest file for an exported runnable jar it will look something more like this (for the same program):
Manifest-Version: 1.0
Rsrc-Class-Path: ./ commons-lang3-3.3.1.jar
Class-Path: .
Rsrc-Main-Class: HelloWorld
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
As you can see, the generated manifest file is failing to include support for third party libraries.
What you would need to do in this case is write your own manifest file and keep it as a resource in your project. Then do a normal jar export, and on the last page of the wizard select the Use existing manifest from workspace option, and point it at your own manifest file.
This will have the advantage that you can export a new runnable jar whenever you like, including your source files, and without requiring manual tinkering each time.
If you're interested in working with manifest files, there's a good tutorial on oracle's website here that can help you get started:
http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html
Please any one give me the suggestion for this. I'm having the Xerces-J-source.zip file and I need to convert to Xerces-J-source.jar file.
Generally if your ZIP contains the class files (And not the sources):
Just rename the file, a jar is a zip file. But if you're aiming for a class to be launched with command java -jar myProject.jar you should create a MANIFEST file containing the main-class and libraries to use in the classpath.
So in your case since you got the sources (Java file), you'll have to compile classes and create the JAR. Eclipse has an Export as JAR feature if you use it.
That doesn't make any sense. If you need Xerces-J the better is download the jars from the website.
If you want the get the jar form the source, you need to compile it, but there is no need when you can download it.
In my java project, i have added some external jars. I want to update a particular jar with my java class file. Please help me how to do this.
Regards
Rajasekaran.G
If the jar you want to add your class to is a 3r party library, I would recommend against doing what you described (it can lead to subtle surprises and unintended side effects).
If the jar you want to update is your own, I would mode the source for the class into the project that generates that jar and it should be added automatically during the generation of the jar.
If you really want to do it by hand, you can unjar the external jar, add your .class file to the resulting directory structure and jar it up again.
You need to update the jar independently try this,
jar -uvf <existingjar> <class file in appropriate folder structure as per package>
e.g.
jar -uvf current.jar NewClass.class
or if your class is in some package say com.my.util, place your class file in com/my/util/NewClass.class and execute the following from prompt
jar -uvf current.jar com
This will update the current.jar file.
If its an external jar, i wonder if you can even do this? If its your own jar, follow the option given by Attila
try to open your .jar file winrar and place your java class wherever you want on the jar.
this will sound silly but i am executing my code from command prompt and have to use a jar in my class.
I have placed my jar in lib folder of JDK..
but i am still getting error of file not found
any explanation??
EDITED : guys tried all but still not working
EDIT 2 :i am trying to work as was told by this link i am using js-1.6R5.jar
Edit 3 : i undestand all the things you ppl have told but nothing working for me.. pls give me a link to upload my example that i can share with you all.
Edit 4 : i am fed up by setting classpaths but its not working... i have SDK installed in my system, do i need an extra JDK to run my programs from command prompt??
You need to add the jar to the class path by doing the following...
java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld
Please see Wikipedia - Classpath_(Java)
You can place it anywhere, as long is you include it in your classpath. See Setting the Class Path for how to include jars in the classpath.
Have in mind that adding something in the JDK lib is almost never a good idea.
You can make a lib folder in your application's directory and put jar files there, then make your application find them by adding lib to your application's classpath.
And, don't put your jar files in JDK's lib folder. It's not good practise.
You need to let Java know that you want to include the jar in your classpath (the list of folders and jars it checks for classes). One way to do this is with the -cp command line argument, something like
java -cp ".;pathToMyJar\myJar.jar" MyClass
Another is to edit the CLASSPATH environment variable in your OS to include your jar.
A simple solution will be to place the jar fiel inside the windows folder if you are doing it in a Windows machine.
Unfortunately your question contains a lot of question signs and few information.
If you are using java.io.File to open jar as a regular file this jar should not be in lib directory. You just have to provide correct path in file system.
If however you are just trying to use jar as a part of your application it should be in classpath. Use either command line java -cp myjar.jar MyMainClassor put full path to this jar to global environment variableCLASSPATH`.
I have updated my ant build.xml file to include a new file and a new folder. After creating the .jar I check if they exist in the jar by 'unzip\extract', and they are there.
But when executing the .jar neither the folder or the file gets extracted.
Am I missing a step?
Look into getResourceAsStream. It'll keep you from having to extract the files from the jar file. Unless that's your goal.
Your application should be able to use the file directly from within the jar, no need for extracting it. Or do you mean something else?
Are you doing something specific to extract the jar file? I ask because normally jar files are not extracted when executing them.
If you run "java -jar myJar.jar" or "java -cp myJar.jar com.example.MyMainClass" the jar files that is referenced will not be extracted. Java will load your classes and resources directly from the jar file without extracting it.
If you wrap your application up using One-JAR, you can specify an attribute in the Manifest file to extract files that you want (See the One-Jar-Expand manifest attribute).
As a bonus, you will also be able to wrap any dependent libraries along with your code, creating a single distributable jar.