4

I have a curious situation. My code does not load the MANIFEST.MF using getResourceAsStream() on OpenJDK 1.6. On Oracle JDK 1.6+ it works well. I came across this when I was accidently running my code on OpenJDK.

main() method:

public static void main(String[] args) throws IOException {

    InputStream is = Main.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF");
    InputStreamReader isr = new InputStreamReader(is);
    char[] c = new char[2048];
    int read;

    StringBuffer b = new StringBuffer();

    while ((read = isr.read(c)) > 0) {
        b.append(c, 0, read);
    }

    System.out.println(b.toString());
}

Maven JAR plugin definition:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>net.noorg.test.Main</mainClass>
                <addClasspath>true</addClasspath>
            </manifest>
        </archive>
    </configuration>
</plugin>

Using OpenJDK 1.6:

$ java -version
java version "1.6.0_18"
OpenJDK Runtime Environment (IcedTea6 1.8.7) (6b18-1.8.7-2~squeeze1)
OpenJDK Client VM (build 14.0-b16, mixed mode, sharing)
$ java -jar target/test-load-resource-1.0-SNAPSHOT.jar
Manifest-Version: 1.0
Created-By: 1.6.0_18 (Sun Microsystems Inc.)

Using Sun JDK 1.6:

$ java -version
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) Client VM (build 20.4-b02, mixed mode, sharing)
$ java -jar target/test-load-resource-1.0-SNAPSHOT.jar
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: ABC
Build-Jdk: 1.7.0_07
Main-Class: net.noorg.test.Main
  • Who's wrong - OpenJDK or I?
  • Whats the reason for this?
  • Is this only related to the Manifest or would this affect all resources?

Edit 1:

  • Both execution use the same .jar
  • Maven is running on Oracle JDK 1.7
  • I'm running a system with two JDKs installed "defining" them trough JAvA_HOME and PATH
  • I'm running the .jar on one machine using different JAVA_HOME's set and PATHs set to java in two different consoles.

In addition to the above outputs

Open JDK (alternative JAVA_HOME/PATH not set):

$ echo "$JAVA_HOME" ; which java

/usr/bin/java

Sun JDK set:

$ echo "$JAVA_HOME" ; which java
/usr/local/jdk
/usr/local/jdk/bin/java

Edit 2:

If the JAVA_HOME is properly defined for the OpenJDK (by default) it's the same.

$ echo "$JAVA_HOME" ; which java
/usr/java
/usr/bin/java

So it's not related to the empty JAVA_HOME on my account.

4

1 回答 1

0

我没有使用过 openJDK 6,也许答案为时已晚,但是我遇到了同样的问题,但使用的是 JDK 1.7.0.2。我看到你正在使用 Maven

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>        
                <configuration>
                  <archive>
                    <manifest>
                       <mainClass>net.noorg.test.Main</mainClass>
                       <addClasspath>true</addClasspath>
                      <classpathPrefix>classes/META-INF/lib</classpathPrefix>              
                    </manifest>
                  </archive>
                </configuration>
            </plugin>

这就是我可以让它运行的方式,我强烈建议您不要使用 openJDK6,我认为随着 JDK 7 的到来,您可以做很多事情并且没有错误风险和更好的安全性能。

于 2013-01-16T17:31:13.163 回答