Add jar library to eclipse project for exporting - java

I have a library called Snakeyaml.jar, and I want to add it to my eclipse project, so it will be included in my jar, when I export my project. So far, I only see ways to add an "External Jar" which only adds a jar library to the buildpath, and does NOT include it in the program when being exported! How can I do it, and do I need any plugins for that? Please help!
Thanks.

Adding the jar to your build path is for compilation and runtime, but from eclipse only. A common misconception is that jar files can be added into other jar files, which will never work. What you probably want is extract your library jar into your exported jar. To achieve this:
File - Export
Expand Java node and select Runnable JAR File
In the library handling section, select Extract required libraries into generated JAR
Reference:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-export-runnable-jar.htm

While exporting you can chose the File->Export->Runnable Jar Option . Then use the selection like below screen i.e Extract required libraries into generated jar

Okay, so I understand what you are actually asking! You want the Snakeyaml.jar file inside of your exported jar file, and for your program to use its libraries.
To do so, drag and drop the Snakeyaml.jar file into your src folder in eclipse.
Then, go to build path, and instead of looking for external jars for your buildpath, choose to use the jar file that is already in your src folder in eclipse. Once you do that, you should export it and pick to "Extract required libraries into jar file" or something like that, and everything should work well! You will notice upon opening the exported jar file with a tool like Winrar, that the jar you had in your src folder is not there, but the packages of the jar are actually side by side with yours.

Related

Manipulate the java class path of an Eclipse plugin?

I am working on a plugin that consist of a homemade view to Eclipse.
When I run the plugin and display the classpath using System.getProperty("java.class.path")
I get this as output : D:\Programs\eclipse\plugins\org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar
I would like to add some .jar files for the proper functioning of my view, but I can't figure out how... I guess you can do it by adding some specifications to the MANIFEST.MF of the plugin but I don't know how to do it propely.
any ideas ?
Each Eclipse plugin has its own classpath. To use additional jars in the plugin you need to include them in the plugin.
Add your jars to the plugin directory. Usually they are put in a 'lib' directory.
Open the plugin MANIFEST.MF editor and on the 'Runtime' tab in the 'Classpath' section click the 'Add...' button and add your jars to the class path.
On the 'Build' tab of the editor make sure the 'lib' folder is include in the Binary Build section.
Your MANIFEST.MF should end up with a `Bundle-Classpath' entry that looks something like:
Bundle-ClassPath: .,
lib/jogg-0.0.7.jar,
lib/jorbis-0.0.15.jar,
lib/vorbisspi1.0.2.jar
(here I have 3 jars in a lib folder).
The build.properties file should be something like:
bin.includes = META-INF/,\
.,\
plugin.xml,\
lib/,\
lib/jogg-0.0.7.jar,\
lib/jorbis-0.0.15.jar,\
lib/vorbisspi1.0.2.jar
For Compile time we need to add it to the Project runtime library.
For the run time you have to package the jar either in your EAR/WAR file or Load it to the Application server as a App server libraries.
Please let me know if you need further assistance on this.
The best approach I've found is to create a lib directory in your Eclipse project (where your view is contained). Place your .jar files in said lib directory.
Then using the editor on the MANIFEST.MF, you add the .jar files to the classpath. If you wish to export the packages, you then add to the Exported Packages as well.
Depending upon what you are doing, you may wish/need to also update the Build Configuration.
If you examine the MANIFEST.MF file itself, you will then see an entry for Bundle-ClassPath. It will list your entries. Here it has the standard "." for the project, a resources/ directory that we export, and a couple of .jar files.
Bundle-ClassPath: .,
resources/,
lib/aopalliance-1.0.jar,
lib/apccore-client-2.11.8.jar,
lib/cglib-nodep-2.2.2.jar,
lib/ehcache-2.10.3.jar,
...
Note that in our experience, it is also necessary to adjust the Java Build Path from the Properties of the project itself. A user commented that this step may not be necessary. We are on an older version of Eclipse due to our product, so YMMV, If needed (usually compile failures are the indicator), you then need to add, via the properties context menu on the project, the .jar files to the "Java Build Path" (you can do the same with a resources directory).
This will allow you to properly build using the .jar files.

Java export jar including libraries

I have been looking around for some time now, but didn't a find way how to export a JAR (not runnable jar) that contains in it's build path the referenced libraries.
Using Eclipse, I have included the lib folder which contains the jars of the referenced libraries in the export process.
Importing that JAR to another project and calling some method results in a ClassNotFoundException.
Looking at the MANIFEST, I didn't see any reference to those jars in the classpath, though the jars are indeed included in the jar.
So my questions are:
1. Is there any way to accomplish the packaging of the non-executable JAR so it will include libraries?
2. Is there any best practice for building and deploying a jar that include other jars libraries?
I tried it too but it doesn't work for me. I added the final .jar file but it doesn't work.
So, I did a workaround.
Extract the .jar file that you want as a dependency.
Copy that content and put it all inside your .jar file.
Add your .jar file as dependency inside an eclipse project.
Run it and see if everything is ok.

Is there any tools help to reverse jar to java eclispe project

All, I have a jar file. and I know it is built from a swing project. Becuase I already view some source code of class files of the jar. I want to know if there exists any tools help to convert the jar back to the source project in the eclipse? Thanks.
http://jd.benow.ca
Drag and drop jar file into JD GUI decompiler
select File-> Save all sources
7zip will allow you to view the contents of the jar, including source files if they are there. Lots of decompilers exist to decompile the class files. My favorite is jd-gui (http://jd.benow.ca/).
You can also add a jar in Eclipse by right-clicking on the Project → Build Path → Configure Build Path. Uthen under tje Libraries tab, click Add Jars or Add External JARs and select the jar file. Alternatively you can add a lib directory to your project and put a copy of the jar there.
But as far as converting a jar file into an eclipse project, you could extract the files with 7zip or other tool, then import them into a new eclipse project.

How to create a jar file of a java project which internall uses some external jars

I want to create a jar file of my project which is internally using some other external jars.
Basically what I am expecting is a single jar file which has all the class files and external files in it.
can someone help me in this
If your classes use external jar files then you need to enclose those jars into your jar file.
Right click on the project in eclipse then :
Click on 'export'->'Runnable Jar`
Here: Make sure you select 'Extract required libraries into generated JAR'
This will extract all the libraries present on the classpath of your project. into the exported jar file

creating jar file for java application

i have created a java application which uses data from its config folder and , it also uses third party jar files those are located in lib folder, could anyone tell me how to create jar file for this project with the content stored in config file and lib folder.
i tried creating jar using eclipse export functionality. when i run this jar file, it says it can not find the third party libraries that i have used for this project and configuration file.
thanks in advance for any help
You can create a Runnable JAR in Eclipse 3.4+ in the Export wizard selection dialog (right click on a project and go to Export...) using an existing launch configuration which will incorporate the libraries or repack them. Config files should be readable from the same directory as the runnable jar is located. If you need any help with loading these in, just ask :)
(source: eclipse.org)
You have two options
include the stuff in the third-party jars in your jar
provide access to the jars on the classpath when you run your jar.
Both have their benefits and their drawbacks.
Java does not support putting JAR files inside executable JAR files, so you can't just put your third-party library JAR files inside your own JAR - Java won't be able to find them.
If you don't want to distribute your application as a whole bunch of JAR files, you can use a look such as One-JAR which will build a JAR file for you that contains your own classes plus the classes of the third-party libraries that you're using.
To learn more about how to package a program in an executable JAR file, see Packaging Programs in JAR Files in Sun's Java Tutorials.
If you use netbeans just by click on "build" a jar file will show up in the "dist" file in your project directory

Categories

Resources