我正在尝试使用 OpenJDK7 根据 OSGi 规范 4.3 编译我的 OSGi 包,但出现错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5:compile (default-compile) on project example: Compilation failure
[ERROR] /tmp/baka/example/src/main/java/org/example/Activator.java:[14,24] error: type ServiceReference does not take parameters
这是我的 Activator.java:
package org.example;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
public class Activator implements BundleActivator {
@Override
public void start(BundleContext bundleContext) throws Exception {
ServiceReference<Runnable> ref = bundleContext.getServiceReference(Runnable.class);
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
}
}
和我的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>example</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Private-Package>org.example</Private-Package>
<Bundle-Activator>org.example.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
当我使用 OpenJDK 6 时不会出现此错误。有什么提示可以让它与 OpenJDK 7 一起工作吗?
最佳答案
您需要使用 Java 7 中的 javac 重新编译 OSGi 源代码。OSGi 使用 -target jsr14 使用 Java 6 javac 编译代码。 Java 7 javac 删除了对针对此类文件进行编译的支持:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7078419
从 R5 开始,OSGi 将不再提供 -target jsr14 类文件。
[2012 年 10 月 31 日更新]
OSGi 现在为 Java 7 提供了重新编译的 4.3 jar 文件。参见 http://blog.osgi.org/2012/10/43-companion-code-for-java-7.html
关于java - 如何使用 OpenJDK 7 编译 mavenized OSGi 4.3 包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10911231/