Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4356783

Running java.exe with "-jar" ignores the classpath

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P5 P5
    • None
    • 1.3.0, 1.4.2
    • hotspot
    • x86
    • windows_2000, windows_xp



      Name: skT45625 Date: 07/26/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)java

      When using the "Main-Class" feature of a jar's manifest file and the "-jar"
      option to java.exe, the classpath environment variable seems to be ignored. I
      have created a testcase:

      Other.java:
      -------------------------------------------------
          public class Other { }
      -------------------------------------------------

      Main.java:
      -------------------------------------------------
          public class Main
          {
              public static void main (String [] args)
              {
                  Other other = new Other();
              }
          }
      ------------------------------------

      manifest:
      ------------------------------------
          Manifest-Version: 1.0
          Main-Class: Main
      -------------------------------------

      Instructions for setting up the test:
      1. Create the files "Other.java", "Main.java", and "manifest" with the contents
      listed above.
      2. javac Main.java
      3. javac Other.java
      4. jar -cf other.jar Other.class
      5. jar -cfm main.jar manifest Main.class

      Now, execute this sequence of commands:

          SET CLASSPATH=other.jar
          java -jar main.jar
          
          SET CLASSPATH=other.java;main.java
          java Main

          java -cp other.jar;main.jar Main

      In the first execution, there is an exception:

          Exception in thread "main" java.lang.NoClassDefFoundError: Other

      even though class Other is in the classpath. The other two commands run
      correctly (no exceptions, no output, as expected). It seems that the "-jar"
      option is causing java.exe to ignore the classpath, which is incorrect: the
      classpath should be used like it is in the last two example executions.

      It is possible to use the "Class-Path" header field name in the manifest to
      work around this issue. This is inappropriate for many applications for because
      the "Class-Path" header field requires the JAR creator to know the location of
      the classes it depends on. The application should not have to know the location
      of the file.

      I noticed this bug when using JAXP. Since I wanted to be able to switch parser
      implementations at runtime (one of the goals of JAXP), I did not know the
      locations of the SAX classes: they could be in "parsers.jar" or "xerces.jar"
      or "xml4j.jar", for example. Because of this I could not put the "Class-Path"
      header in my JAR file to reference any one of these. I can send a more
      elaborate testcase that uses JAXP to demonstrate this problem if needed.
      (Review ID: 107498)
      ======================================================================

            foliversunw Fred Oliver (Inactive)
            skondamasunw Suresh Kondamareddy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: