【问题标题】:Code Compiles in Eclipse but not through command prompt代码在 Eclipse 中编译,但不是通过命令提示符
【发布时间】:2012-12-18 05:40:43
【问题描述】:

以下代码在使用 JDK7 的 Eclipse 中编译没有任何问题(我使用的是更新 10,但应该可以使用任何版本的 JDK7 重现),但在使用完全相同的 JDK 通过命令行编译时会失败。该类仅提供接口方法的存根实现。

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.Version;


public class TestBundle implements Bundle {

    /**
     * @param args
     */
    public static void main(String[] args) {


    }

    @Override
    public int compareTo(Bundle o) {

        return 0;
    }

    @Override
    public <A> A adapt(Class<A> arg0) {

        return null;
    }

    @Override
    public Enumeration<URL> findEntries(String arg0, String arg1, boolean arg2) {

        return null;
    }

    @Override
    public BundleContext getBundleContext() {

        return null;
    }

    @Override
    public long getBundleId() {

        return 0;
    }

    @Override
    public File getDataFile(String arg0) {

        return null;
    }

    @Override
    public URL getEntry(String arg0) {

        return null;
    }

    @Override
    public Enumeration<String> getEntryPaths(String arg0) {

        return null;
    }

    @Override
    public Dictionary<String, String> getHeaders() {

        return null;
    }

    @Override
    public Dictionary<String, String> getHeaders(String arg0) {

        return null;
    }

    @Override
    public long getLastModified() {

        return 0;
    }

    @Override
    public String getLocation() {

        return null;
    }

    @Override
    public ServiceReference<?>[] getRegisteredServices() {

        return null;
    }

    @Override
    public URL getResource(String arg0) {

        return null;
    }

    @Override
    public Enumeration<URL> getResources(String arg0) throws IOException {

        return null;
    }

    @Override
    public ServiceReference<?>[] getServicesInUse() {

        return null;
    }

    @Override
    public Map<X509Certificate, List<X509Certificate>> getSignerCertificates(
            int arg0) {

        return null;
    }

    @Override
    public int getState() {

        return 0;
    }

    @Override
    public String getSymbolicName() {

        return null;
    }

    @Override
    public Version getVersion() {

        return null;
    }

    @Override
    public boolean hasPermission(Object arg0) {

        return false;
    }

    @Override
    public Class<?> loadClass(String arg0) throws ClassNotFoundException {

        return null;
    }

    @Override
    public void start() throws BundleException {


    }

    @Override
    public void start(int arg0) throws BundleException {


    }

    @Override
    public void stop() throws BundleException {


    }

    @Override
    public void stop(int arg0) throws BundleException {


    }

    @Override
    public void uninstall() throws BundleException {


    }

    @Override
    public void update() throws BundleException {


    }

    @Override
    public void update(InputStream arg0) throws BundleException {


    }

}

osgi的jar文件可以从here下载

我正在使用以下命令通过命令行编译它:

javac.exe -cp org.eclipse.osgi_3.8.0.v20120529-1548.jar TestBundle.java

通过命令行编译时出现以下错误:

TestBundle.java:101: error: type ServiceReference does not take parameters
        public ServiceReference<?>[] getRegisteredServices() {
                               ^
TestBundle.java:119: error: type ServiceReference does not take parameters
        public ServiceReference<?>[] getServicesInUse() {
                               ^
TestBundle.java:18: error: TestBundle is not abstract and does not override abstract method adapt(Class) in Bundle
public class TestBundle implements Bundle {
       ^
TestBundle.java:28: error: method does not override or implement a method from a supertype
        @Override
        ^
TestBundle.java:35: error: name clash: <A>adapt(Class<A>) in TestBundle and adapt(Class) in Bundle have the same erasure, yet neither overrides the other
        public <A> A adapt(Class<A> arg0) {
                     ^
  where A is a type-variable:
    A extends Object declared in method <A>adapt(Class<A>)
TestBundle.java:34: error: method does not override or implement a method from a supertype
        @Override
        ^
6 errors

【问题讨论】:

  • 你检查了eclipse项目目录下的.classpath文件了吗?它为您提供了要包含在类路径中的所有 jar 文件
  • @BhavikShah:是的,我做到了。它仅包含 JDK 和上述 jar 文件的条目。

标签: java generics osgi


【解决方案1】:

OSGi 规范包在 Java 7 上编译时存在泛型问题。这是因为编译后的包具有对 jdk 1.4 的向后兼容性,这使得它们在 Jdk 7 中中断。经过大量投诉后,发布了一个新版本:现在兼容 jdk 7。

4.3.1 源与 4.3.0 相同。它只是重新编译。你应该可以用这个 jar 编译你的代码。我不确定这与您使用的 eclipse 中的 jar 有何关系,但我猜他们只是使用了旧的编译规范类。

http://search.maven.org/remotecontent?filepath=org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar

【讨论】:

  • 只是为了补充这个答案,“为什么上面的代码在eclipse中编译”是因为eclipse使用JDT编译器而不是javac。
【解决方案2】:

我能够使用 javac *从命令行* 在从 jar 中删除一些其他(我想是与 eclipse 相关的)条目并只保留类之后编译代码和包裹。 jar 的清理版本是here

我还删除了eclipse通常引入的@Override注解。

【讨论】:

  • 是的,我们可以通过调整 jar 使其工作,但问题是为什么它不能按原样工作。关于@Override,它实际上很有用,应尽可能使用。见stackoverflow.com/questions/94361/…
  • 存在与@Override 滥用有关的问题。经常发生的情况是,我们没有以传统方式重写方法(即在类中实现一个实际实现而不是重写的接口方法),因此编译器会抱怨。
  • 就 jar 的调整而言,一旦您知道 jar 内部有问题,就可以轻松缩小问题的范围。我说的对吗?
  • 是的,但奇怪的是它在 eclipse 中工作,没有任何变化。
  • 我想它需要进一步检查。
【解决方案3】:

你提到了错误的jar文件名-“org.eclipse.osgi_3.8.0.v20120529-1548.jar” 正确的文件名是 org.eclipse.osgi-3.8.0.v20120529-1548.jar

这个命令: javac.exe -cp org.eclipse.osgi-3.8.0.v20120529-1548.jar TestBundle.java

为我工作

【讨论】:

  • 你使用的是什么JDK版本?
猜你喜欢
  • 2020-08-08
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-22
  • 2016-01-15
  • 1970-01-01
相关资源
最近更新 更多