3

The following code compiles without any problem in Eclipse using JDK7 (I am using update 10, but should be reproducible with any version of JDK7), but fails when compiled through command line with exact same JDK. The class just provides stub implementation of interface methods.

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 {


    }

}

The jar file for osgi can be downloaded from here

I am using the following command to compile this via command line:

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

It gives following error when compiled through command line:

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
Manish
  • 3,834
  • 2
  • 25
  • 42
  • did you check the .classpath file in the eclipse project directory? it gives you all the jar file to be included in your class path – Bhavik Shah Dec 18 '12 at 05:46
  • @BhavikShah: Yes I did. It only contains entries for the JDK and the above mentioned jar file. – Manish Dec 18 '12 at 05:48

3 Answers3

12

The OSGi spec bundles have an issue with generics when compiled on Java 7. This is because the bundles where compiled with backwards compatibility for jdk 1.4 which makes them break in Jdk 7. After a lot of complaints a new version was released that is compliant to jdk 7 now.

The 4.3.1 sources are identically to 4.3.0. It is just recompiled. You should be able to compile your code with this jar. I am not sure how this relates to the jar from eclipse you used but I guess they just used the old compiled spec classes.

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

Christian Schneider
  • 18,427
  • 2
  • 33
  • 56
  • Just to add to this answer, "why the above code compiles in eclipse" is because eclipse uses JDT compiler rather than javac. – Manish Dec 24 '12 at 04:14
0

I was able to compile the code using javac *from command line* after i removed some other (eclipse related i suppose) entries form jar and kept only the classes and packages. Cleaned version of jar is here.

I also removed the @Override annotations which are usually introduced by eclipse.

Amber
  • 1,195
  • 8
  • 26
  • Yeah we can make it work by tweaking the jar, but the question is why it doesn't work as it is. Regarding @Override, its infact useful and should be used whenever possible. See http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why – Manish Dec 18 '12 at 06:16
  • There are issues related to @Override misuse. It happens quire often that we have not overridden a method in Traditional manner (i.e. implementing a interface method in class which is actually implementing and not overriding) and hence the compiler complains. – Amber Dec 18 '12 at 06:20
  • As far as the tweaking with jar is concerned the issue can be easily narrowed down once you know that there is something wrong inside the jar. Am i correct ? – Amber Dec 18 '12 at 06:21
  • Yeah, but strange thing is it works inside eclipse without any changes. – Manish Dec 18 '12 at 06:33
  • It requires further checking i suppose. – Amber Dec 18 '12 at 06:37
0

You have mentioned wrong jar file name -"org.eclipse.osgi_3.8.0.v20120529-1548.jar" Correct file name is org.eclipse.osgi-3.8.0.v20120529-1548.jar

this command : javac.exe -cp org.eclipse.osgi-3.8.0.v20120529-1548.jar TestBundle.java

is working for me

Prateek Sharma
  • 348
  • 3
  • 11