JDK-5087182 : A jar executed with "java -jar" is not able to access classes in the classpath
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-08-16
  • Updated: 2004-11-17
  • Resolved: 2004-11-17
Related Reports
Duplicate :  
Description

Name: js151677			Date: 08/16/2004


FULL PRODUCT VERSION :
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
When using the "-jar" option of the "java" command to run the Main-Class of a jar, it appears classes loaded from the jar can not use the jars specified in the classpath with the "-cp" option. At least using "Class.forName" calls result in ClassNotFoundException for classes in the normal classpath.

See "Steps to Reproduce" for a testcase.

What should be used instead of Class.forName?

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect the classes added to the standard classpath to be available to the classes inside the jars using the normal "Class.forName" as I did not explicitely used any custom classloader.
ACTUAL -
An exception is thrown when using Class.forName.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
Use these two classes and the manifest:

src/mypackage/A.java

package mypackage;

public class A {

  public static void main(String[] args) throws Exception {
    Class.forName("mypackage.B");
  }

}

src/mypackage/B.java

package mypackage;

public class B {
}

src/manifest.mf

Main-Class: mypackage.A

Once compiled, build A.jar with mypackage/A.class and manifest.mf:
jar cvfm A.jar manifest.mf mypackage\A.class
build B.jar with mypackage/B.class:
jar cvf B.jar mypackage\B.class

now run "java -cp A.jar;B.jar mypackage.A". It runs correctly.

if you run "java -cp B.jar -jar A.jar" it throws ClassNotFoundException mypackage.B.
---------- END SOURCE ----------
(Incident Review ID: 296132) 
======================================================================

Comments
EVALUATION Investigate for Dragon. ###@###.### 2004-08-17 Is this a TL issue? ###@###.### 2004-11-11 16:18:12 GMT This is the documented behavior when using -jar; from the jdk docs for -jar "When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored." ###@###.### 2004-11-17 00:08:19 GMT
17-11-2004