Manipulate the java class path of an Eclipse plugin? - java

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.

Related

Add jar library to eclipse project for exporting

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.

Eclipse plugin cannot find class from class folder

I'm developing an Eclipse plugin, and want to use some pre-compiled classes which I've put to lib/ folder in my project (so the directory structure looks like {proj_root}/lib/com/example/Required.class). Then I've added lib/ folder as class folder to project's build path. At build time all is working fine, I can import and use that classes in my code. And now the problem is, when I'm running my plugin in separate Eclipse instance I get ClassNotFoundException by my plugin refering to class from precompiled classes folder.I tried specifying my class folder in "Order and export" tab in Build path settings. Also I tried setting checkbox in plugin's build.properties GUI editor in front of lib/ folder to be included in binary build. In that case if I export my plugin to JAR I can see lib/ folder inside it, with my classfiles, but at runtime my plugin still cannot find any of classes from lib/.Can you please give me an advice on how I should tell Eclipse where to find that classes at runtime?
PS.:
I did research on stackoverflow on this problem, but all suggestions are the same: just add class folder in project's Build path.
At this point in plugin's build.properties Text editor, eclipse shows warning The class folder 'lib/' is not associated to any output library entry. and suggests to make this modification of code:
output.. = bin/,lib/. I have tried that solution but it is seems to change nothing with the problem.
It sounds very much like you have added the lib folder to the "Java Build Path" properties page of the plug-in project.
But... for plug-in projects, this does not work as these are OSGi bundles - you must include the lib folder in the PDE Editor on the "Runtime" page:

How to add a JAR to an Eclipse plugin I am developing?

I am developing an eclipse plugin. It has other plugin/feature dependencies. However I want to add my own favorite JAR/libriaries to it, like say logback for example. How can I do this so that when I eventually deploy it to an update site, it will have these jars on the classpath?
Also, I am currently using eclipse to run the plugin (it opens another instance of eclipse) to run this. This will also need to have the jar on the classpath.
You can try these steps :-
Use Import>File System to import the jar files into your plugin project, say in the /lib directory.
Use "Add..." to add the jars to the classpath section of the plugin.xml>Runtime tab.
Use "New..." to add "." library back (with no quotes, of course).
make sure your binary build exports the new jar files on the plugin.xml>Build tab.
save.
on the project, use context menu>PDE Tools>Update Classpath to correctly add the jars to the eclipse project classpath.
In plugin-manifest editor select Runtime tab and add you jar in classpath section.

How to convert Eclipse RCP plugin JAR to a plugin directory and vice versa?

What I have is an RCP application, and a plugin directory in it:
my-rcp-app/
...
plugins/
...
com.mycompany.myproduct.ui_1.0.1/
com/
META-INF/
plugin.xml
Then I make a JAR with the same contents as the plugin directory, com.mycompany.myproduct.ui_1.0.1.jar, and put it into plugins/ instead of a com.mycompany.myproduct.ui_1.0.1 folder.
Surprisingly, the app doesn't start. That's from the logs:
!SUBENTRY 2 com.mycompany.root.bundle 2 0 2012-07-05 20:01:17.511
!MESSAGE Missing required bundle com.mycompany.myproduct.ui_1.0.1.
So, 2 questions here:
How to deploy a plugin in a JAR, if I've got a directory with classes, configs etc.
Is it enough to unpack a JAR into plugins, for the app to keep working?
From the main page of the Manifest Editor you can open an export wizard. It will create a jar file containing your plug-in. If you want to specify which files should be exported, then go to the build tab in the Manifest Editor.
It should not matter if your plug-in is unpacked or in a Jar File. Equinox is able to handle both cases. Note that is recommended to use the dropins folder instead of the plugins folder, if you don't want to manage plug-ins via the Eclipse Update Site mechanism.
As #TobiasWillig already noted, plug-in directories and JARs are indeed fully interchangeable.
But in this specific case I had to edit configuration/org.eclipse.equinox.simpleconfigurator/bundles.info file, replacing
com.mycompany.myproduct.ui,1.0.1,plugins/com.mycompany.myproduct.ui_1.0.1/,4,false
with the
com.mycompany.myproduct.ui,1.0.1,plugins/com.mycompany.myproduct.ui_1.0.1.jar,4,false
I.e., it's required to manually specify a path to the plug-in.

External jars in eclipse plug-in

I'm creating an eclipse plug-in and I'm having trouble with external jars. In my plug-in I start an application which requires some external jars. What do I have to do to export them automatically with the rest of the plug-in?
Thanks in advance :)
Open your plugin.xml
Go to Runtime tab and add your JAR in the classpath section
By following the steps below, the external JARs will be included when you export your plug-in:
Copy the required external JAR files into a folder in your plug-in project; I like to call this folder lib, but whatever works for you.
Open the build.properties in your plug-in project and check the JARs you want to include in the build in the Binary Build section on the left.
This is an easy way, though it does create an additional plug-in.
In Eclipse:
Choose New>Other, then Plug-in Development>Plug-in from existing JAR archives.
Choose the jars you want to include.
On the next page, configure the plug-in.
In your plug-in's manifest, add the new plug-in as a dependency.
In your plug-in's Properties, add the new project under Java Build Path on the Projects tab.
Edit: You may be able to combine the jars into your plug-in by instead combining both suggestions of Zsolt and user714965 below.
This is what has worked for me. If they are truly external, and this is a project for your company that is not going to be in the "wild" and you control the environment, and you have them out on the file system say at /opt/java/lib/somedir/some.jar you can tell the bundle where to find them by adding them to the Bundle-ClassPath entry in the MANIFEST.MF under META-INF. The syntax is:
Bundle-ClassPath: .,
external:/opt/java/lib/somedir/some.jar,
external:/opt/java/lib/someotherdir/someother.jar
Also it would be prudent to use these in your build path so that you are working with the same jars in both build and runtime environments.
I have done this where the Jars are multi-purpose (such as apache-commons) on our file systems and again we control the environment.
Reference the following:
http://www.eclipsezone.com/eclipse/forums/t51870.html
Copy the required external JAR files into a folder in your plug-in project; I like to call this folder lib, but whatever works for you.
..but as apposed to Zsolt Török (his solution did not work for me) I double-clicked on the plugin.xml, went to the build tab, at the bottom, I clicked Add JARs..., and hey presto, it showed my project, and I then navigated to lib and included the external jar, as per step 1.
The approach from Andy Thomas mostly works, but you also need to export the packages needed by the existing plugin in the new wrapper plugin so that they can be accessed by the plugin at runtime.

Categories

Resources