简体   繁体   中英

How can I call a method in jar from another eclipse plugin project?

I want to call a method in jar from another eclipse plugin project that has dependent to the project. But eclipse can't resolve the classname in jar.

Example:

I created com.plugin.main and com.plugin.sub project.

  • Added poi-ooxml-xxx.jar into com.plugin.sub project and added the buildpath through preference dialog.
  • Added all packages in jar as exported packages to MANIFEST.MF in com.plugin.sub project .
  • Added com.plugin.sub as required plugin to MANIFEST.MF in com.plugin.main project.

But eclipse can't resolve the classname in jar WorkbookFactory from com.plugin.main.actions.SampleAction. Why?

项目信息

Information:

  • eclipse 3.6(Helios)
  • JavaSE-1.7.
  • WorkbookFactory 's FQCN is org.apache.poi.ss.user.model.WorkbookFactory .

MANIFEST.MF in com.plugin.main :

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Main
Bundle-SymbolicName: com.plugin.main; singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.plugin.main.Activator
Bundle-Vendor: PLUGIN
Require-Bundle: com.plugin.sub;visibility:=reexport,
 org.eclipse.ui,
 org.eclipse.core.runtime
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy

MANIFEST.MF in com.plugin.sub :

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Sub
Bundle-SymbolicName: com.plugin.sub
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.plugin.sub.Activator
Bundle-Vendor: PLUGIN
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Export-Package: org.apache.poi.ss.usermodel
Bundle-ClassPath: poi-ooxml-3.8-beta3-20110606.jar,
 .

build.properties in com.plugin.sub :

source.. = src/
output.. = bin/
bin.includes = META-INF/,\
               .,\
               poi-ooxml-3.8-beta3-20110606.jar

Try these:

  1. Open the manifest files of both the project in Manifest Editor . Go to Runtime tab. In the Class path section check the entries. Add . (means current folder) if not exist. If . entry exists then move it on top. Save the editor and check.

  2. Right click on com.plugin.main project and go to Properties. Go to Java Build Path . On right hand side go to Project tab. Then add com.plugin.sub project.

If adopted point 2 then remember test the application after exporting and running outside eclipse.

Wrong way of Add jars to project

You can add jars through Add JARs button in Library tab in Preference Dialog.

在此输入图像描述

Then, .class file in the project is edited automatically as follows:

<classpath>
    ...
    <classpathentry kind="lib" path="poi-ooxml-3.8-beta3-20110606.jar"/>
</classpath>

But the added tag should has exported attribute as follows:

<classpath>
    ...
    <classpathentry exported="true" kind="lib" path="poi-ooxml-3.8-beta3-20110606.jar"/>
</classpath>

It isn't generated automatically. You have to add the tag in Order and Export tab by hand.

Correct way of Add jars to project

In eclipse, you shouldn't edit Java Build Path manually. You should use Plug-in Manifest Editor because .class is edited automatically when you edit a data through the editor. And also build.properties , plugin.xml , MANIFEST.MF will be edited correctly and automatically.

When you add jars to the project, you have to use Classpath section in Runtime tabs in Plug-in Manifest Editor. When add jars through the section, classpathentry tag with export attribute will be added in .class file. Not only that, the jar will be added as binary includes in build.properties.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM