How to export an Eclipse Project with External Jar dependencies? - java

In Processing, I'd like to import a library I've written in Java which wraps around an external library (a .jar file).
Processing appears to import my own library, but when I call the constructor (which references some classes in the external library), I get a java.lang.NoClassDefFoundError in the Processing GUI console.
If instead of including the External .jar in my Eclipse buildpath, I use the library's actual source code and export all of that, Processing does not complain.
How might I be able to package up this project so that I don't have to include all of the library's source code (and instead just include the .jar file) in my Eclipse project?
Update: As per N.L.telaviv 's suggestion, I chose to export as a Runnable JAR. What worked for me was to select the option "Copy required libraries into a sub-folder next to the generated JAR." Here, another folder is created next to where your .jar is output containing the referenced .jar libraries.
Update2: The other option which works is "Extract required libraries into generated JAR." Doing so allows the JAR to access the references internally. It seemed as though the same would have worked with N.L.telaviv 's suggestion, but that does not appear to be the case. Choose this option if you don't want to have any extra files. You'll notice, if you explore the .JAR file, that the external library is in a separate package folder.

Use the Runnable JAR file export option.
Click on your project - export - java- runnable jar file - select the option to pack requires libraries in the jar

Related

Eclipse: Excluding some libraries while extracting others on export to Runnable JAR

I'm building a java project that implements Twitter Storm, and I want to extract and package in my custom libraries into the .jar file. However, if I extract all the required libraries (using the Extract required libraries into generated JAR, the .jar ends up being 20mB in size, as opposed to around 200kBs (using the Copy required libraries into a sub-folder next to the generated JAR option).
Is there any way I can specify which libraries I want to be packaged in? Ideally, I'd be able to select these to be extracted into the .jar file, while the rest are excluded. I can't seem to find an option in Eclipse to do this.
Thanks very much
I think your best bet is to use the option to "Save as Ant script" and then edit the build.xml to remove the jar files that you don't want to have included in.
Dealing with build.xml files in Eclipse is very easy/natural. If you do show view/Ant, and then drag the build.xml from the package explorer to the ant view, you can just double click on it. Much easier than going through the export dialog e very time.

How to export java project as jar file

I have made a utility java project which contains XML, DB and other such utility classes. This requires 3rd party dependencies on some of the Apache common libraries, so I have added it in my java project by configuring the build path in eclipse Juno. Now I want to export this java project as jar file. When I am exporting this project as runnable jar file, it is working fine i.e if I include this jar in some other java project, I am able to access the utility classes, but when I am simply exporting the utility project as jar, I am not able to use it. Runnable jar requires a main class, but I don't want to keep a main class in my utility java project. I have compared both the jar files. The difference that I found out was that in runnable jar file, there is no .classpath file but a simple jar file that is in there. In the runnable jar file, all the jar files are mentioned in file named MANIFEST-INF.mf file, but in simple jar file it contains only version of .mf file. Can anyone tell me how can I make a jar file without a main class and use it for my other java projects so that I just have to include the jar file and use it as it is.
I assume it has not been provided in eclipse because extracting external 3rd party library classes in a utility project is something non-standard. It can result in a lot of problems as your project grows, class conflicts being one of them.
Still if you want to continue with your approach, yon can export your project as a normal jar project. Thereafter after open your project as well as the other third party libs in a utility like 7zip and drag-and-drop your third party library contents(except META-INF) into your project jar. That's all.
If you however want to automate it, ant would be your friend.
If you just export the project to jar file, you can package the jar(war) by using the apache ant tool.
Do something like this:
<jar destfile="${dist}/lib/app.jar"
basedir="${build}/classes"
excludes="**/Test.class"
/>`

Combine or merge all jars in one executable jar

Kind of a "simple" question here, but it seems complicated to do such a thing using Eclipse.
I have a "utils" project, in which I have developped "common" code like xml parsers, loggers, maths calculations, debug utilities and such.
This library is based on several other external libs (commons-lang-3.1, colt-1.2.0, jdom-2.0.4) to work, and it's a non-runnable JAR file (ie, there is no main(), just utility code to include in other projects).
What I want to do is, when I create the JAR file of my project, extract all external JARs (common-lang, colt, jdom) in the JAR file, in order to automatically use them on other projects.
The reason is that I don't want to re-include common-lang, colt and jdom on every projects based on my lib, but use the ones packed in my lib JAR file (and it's a way to ensure that I'll be using the same version of those libs in the projects based on my lib, too)
I know how to do that with a runnable JAR file.
See for instance : How to create a jar with external libraries included in Eclipse?
But in a non-runnable JAR file :
all my classes are correcly exported in a correct directory structure based on the packages I created (hopefully ;-)),
but external JAR files are exported "as-is", so when I import my JAR in another project, those JARs (common-lang, ...) can't be seen, and I have to re-import them.
Is there a simple way to export this JAR file with a directory structure in which all external JARs files are extracted in the generated JAR file, too ?
Regards
Have you seen JarSplice?
JarSplice merge all your jars and native files into one, easy to use executable jar file via an easy to use GUI.

How to use Java API's?

Probably really simple but I just cannot work out how to use any API's with Java (Using Eclipse).
Where do they go?
For example I want to use a Twitter API and I import it using:
import net.unto.twitter.Api;
I then include the API file in the same dictionary as my class file. (This is what I do in python)
Anyone care to tell me the simple answer?
Typically APIs are packaged in so-called JARs, which stands for Java ARchive. What you should do is:
Download the jar.
Put it in some sort of 'lib' directory in your project structure. Do not put it with your source code.
Add the jar to your classpath. Some IDEs you have to add jar by jar, others you tell the IDE which directories are library directories and it will modify the classpath for you to include all the jars in the directory.
Import the relevant package in your files where you use the API.
Once you have the jar added to your classpath and import the relevant classes, Eclipse should automatically pick it up and allow you to use autocomplete features against the API.
It is also a good idea to get the source jar for any libraries you download. Most IDEs let you attach src jars, so you can click into the API and see how the code is written.
You need to include any external JARs in the build path. In eclipse right click on the project and go to 'Build Path' > 'Configure Build Path' then go to the 'Libraries' tab and 'Add JARs'. Also, when using eclipse it is easier not to maintain the import statements yourself, but instead use 'Organize Imports' (ctrl + shft + o) or Import the selected object (ctrl + shft + m). If a matching object is available in the classpath it will be imported.
Java APIs are distributed in JAR files (which are just zip files with a specifiec directory structure).
Download the jar in question, put it in a known location on your disk (known location as in, pay attention to the name of the direcctory where you store the downloaded jar).
There are two times you will need to reference a Jar file:
During project compilation. This includes development of the project using an IDE like eclipse (they all compile the code to find errors).
Option 1, add an external jar:
Open the project properties in Eclipse
Select Java Build Path
Select the Libraries tab
Click the Add External Jar button
Browse to the location of the JAR in question.
Select the JAR.
Click the Open button.
Option 2, add the JAR to your web project's WEB-INF/lib directory.
During project execution. This will be after you deploy your application to a web container (perhaps tomcat). Store the JAR in question in the classpath of the appliction post deploy. For simple web applications, you can put the jar in the project WEB-INF/lib directory.
You should have it included in the classpath (build path) of the project.

Java: export to an .jar file in eclipse

I'm trying to export a program in Eclipse to a jar file.
In my project I have added some pictures and PDF:s. When I'm exporting to jar file, it seems that only the main has been compiled and exported.
My will is to export everything to a jar file if it's possible, because then I want to convert it to an extraditable file, like .exe-file.
But how?
No need for external plugins. In the Export JAR dialog, make sure you select all the necessary resources you want to export. By default, there should be no problem exporting other resource files as well (pictures, configuration files, etc...), see screenshot below.
Go to file->export->JAR file, there you may select "Export generated class files and sources" and make sure that your project is selected, and all folder under there are also! Good luck!
FatJar can help you in this case.
In addition to the"Export as Jar" function which is included to Eclipse the Plug-In bundles all dependent JARs together into one executable jar.
The Plug-In adds the Entry "Build Fat Jar" to the Context-Menu of Java-projects
This is useful if your final exported jar includes other external jars.
If you have Ganymede, the Export Jar dialog is enough to export your resources from your project.
After Ganymede, you have:
One more option is WinRun4J. There is an Eclipse Plugin for WinRun4J that allows you to export your application as a single executable with necessary jars/classes embedded.
(full disclosure: I work on this project)

Categories