1

I have the following problem with Java9 using the below simple application. The application has the main class Main:

package app;

import lib.Greeting;

public class Main {
    public static void main( String[] args ) {
        System.out.println( new Greeting().hello() );
    }
}

The class Greeting is the following and packaged in the separate jar library lib.jar:

package lib;

public class Greeting {
    public String hello() {
        return "Hi from Greeting lib!";
    }
}

The application Main and lib.jar are packaged by Eclipse to the app.jar library with the manifest file as follows:

**Manifest-Version: 1.0
Rsrc-Class-Path: ./ lib.jar
Class-Path: .
Rsrc-Main-Class: app.Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader**

When I run this application with Java8, it works as expected:

C:\java\J9Test\libs>java -jar app.jar
Hi from Greeting lib!

When I run this application with Java9, the following exception is raised:

C:\orawork\java\J9Test\libs>d:\jdk1.9\bin\java.exe -jar app.jar
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: lib/Greeting
        at app.Main.main(Main.java:7)
        ... 5 more
Caused by: java.lang.ClassNotFoundException: lib.Greeting
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:563)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
        ... 6 more

How to resolve this problem and run this simple app successfully with both Java8 and 9?

Jack Flamp
  • 1,223
  • 1
  • 16
  • 32
michael
  • 17
  • 3
  • check out the answer to this question: https://stackoverflow.com/questions/43606502/how-to-add-a-java-9-module-via-manifest-mf – wleao Nov 28 '17 at 08:58
  • Please maintain the classpath for java( Java_Home) – Lova Chittumuri Nov 28 '17 at 10:16
  • While executing the jar with Java9 make sure that the jar is found either in the classpath or the modulepath. Adding `--module-path ` to the command shall solve it. – Naman Nov 28 '17 at 10:42
  • thank you. how can I use --module-path if this lib.jar is contained in the app.jar of the application? – michael Nov 28 '17 at 11:06
  • @michael Could you share the app.jar structure. I didn't get the previous comment. – Naman Nov 28 '17 at 13:27

1 Answers1

0

Apparently, the Eclipse-specific technology to create executable jars by including dependencies as nested jars (see "Library handling") is not fully adjusted to Java 9 as of Eclipse Oxygen.1a.

According to this comment this has been fixed in Oxygen.2 which will be released on 2017-12-20 (release candidates exist).

Stephan Herrmann
  • 7,963
  • 2
  • 27
  • 38
  • Right. Thank you, Stephen! I already discovered this and decided to include extracted classes to the target app jar instead of lib jars. – michael Dec 04 '17 at 09:31