Starting java with -jar option precludes *.class files on CLASSPATH ?

Discussion in 'Java' started by Lukas, Nov 29, 2005.

  1. Lukas

    Lukas Guest

    Hi group,

    Since I found the documentation unclear on the issue, I ran some tests
    to see whether I could get a directory with *.class files onto the
    CLASSPATH when starting a JARred Java app (java -jar someApp.jar),
    using either the JARs Manifest file, java's -classpath option or by
    setting the CLASSPATH environment variable in a batch file.

    It appears it can't be done.

    A) can anyone confirm this?

    B) are there any known reasons for this seemingly arbitrary
    restriction?

    Cheers

    YT
     
    Lukas, Nov 29, 2005
    #1
    1. Advertising

  2. Lukas

    Andrew E Guest

    Re: Starting java with -jar option precludes *.class files on CLASSPATH?

    Lukas wrote:
    > Hi group,
    >
    > Since I found the documentation unclear on the issue, I ran some tests
    > to see whether I could get a directory with *.class files onto the
    > CLASSPATH when starting a JARred Java app (java -jar someApp.jar),
    > using either the JARs Manifest file, java's -classpath option or by
    > setting the CLASSPATH environment variable in a batch file.
    >
    > It appears it can't be done.


    if you do this:

    java -cp C:\wherever\classes -jar someApp.jar

    dooes that work?
    I don't have a java-installation in front of me to test, but I'm sure that used
    to work for me. But I could be wrong..
     
    Andrew E, Nov 29, 2005
    #2
    1. Advertising

  3. Lukas

    Lukas Guest

    I used relative paths, and found it doesn't work.
    ... Didn't test for absolute paths because for my purpose they'd not cut
    it, but I can't see why absolute vs relative should make a difference.

    Also I've tried

    set CLASSPATH=whereever;.

    and in Manifest.mf

    Class-Path: whereever .

    They all don't work with -jar. As soon as you do

    java -cp C:\wherever\classes someApp.class

    everything works.
    I'd rather be wrong about this though.
     
    Lukas, Nov 29, 2005
    #3
  4. Lukas

    Timbo Guest

    Re: Starting java with -jar option precludes *.class files on CLASSPATH?

    Lukas wrote:
    > Hi group,
    >
    > Since I found the documentation unclear on the issue, I ran some tests
    > to see whether I could get a directory with *.class files onto the
    > CLASSPATH when starting a JARred Java app (java -jar someApp.jar),
    > using either the JARs Manifest file, java's -classpath option or by
    > setting the CLASSPATH environment variable in a batch file.
    >
    > It appears it can't be done.
    >
    > A) can anyone confirm this?
    >

    Yes, the -cp option is ignored when the -jar option is used

    > B) are there any known reasons for this seemingly arbitrary
    > restriction?
    >

    Because jars are designed to be self contained.

    The easiest way around it is:
    java -cp normal_class_path:someApp.jar the,main.method.Class

    It does mean the main method property in the manifest file is
    ignored, but if you rely on some third-party app that can't be
    jarred up with your app, then I'm afraid your stuck with this, or
    something similar.

    Cheers,
    Tim
     
    Timbo, Nov 29, 2005
    #4
  5. Lukas

    Lukas Guest

    Hi Tim,

    that's how I'm currently dealing with the problem, and yes, it works
    just fine, it only looks kinda unprofessional to have a full

    com/somecompany/someproject/EntryPoint.class

    lying around in your install directory when you were hoping to just
    have

    lib/someproject.jar

    looking neat and tidy.
    Hybris, I guess.

    Thanks,

    Lukas
     
    Lukas, Nov 29, 2005
    #5
  6. Lukas

    Timbo Guest

    Re: Starting java with -jar option precludes *.class files on CLASSPATH?

    Lukas wrote:
    > Hi Tim,
    >
    > that's how I'm currently dealing with the problem, and yes, it works
    > just fine, it only looks kinda unprofessional to have a full
    >
    > com/somecompany/someproject/EntryPoint.class
    >

    You don't need to have the classes unjarred into the install
    directory. If you include the jars from your app and the third
    party app in the -cp option and then just provide the name of the
    class with the 'main' method:
    java -cp thirdparty.jar:yourApp.jar your.main.Class

    then your classpath becomes all of the classes in both jar files.
    It's still not ideal, but it's better than having the classes
    jarred our into the install directory.

    Hope this helps!
    Tim
     
    Timbo, Nov 29, 2005
    #6
  7. Lukas

    Roedy Green Guest

    On Tue, 29 Nov 2005 13:56:47 +0100, Andrew E <>
    wrote, quoted or indirectly quoted someone who said :

    >if you do this:
    >
    > java -cp C:\wherever\classes -jar someApp.jar
    >
    >dooes that work?


    when you use a jar, the classpath is ignored. Sun assures us this is
    not a bug but a feature.

    see http://mindprod.com/jgloss/classpath.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 29, 2005
    #7
  8. Lukas

    Lukas Guest

    Indeed that helped! Apparently some misconception about the -jar option
    had got stuck in my head there.

    Thanks!

    Lukas
     
    Lukas, Nov 30, 2005
    #8
  9. Lukas

    Nigel Wade Guest

    Lukas wrote:

    > Hi group,
    >
    > Since I found the documentation unclear on the issue, I ran some tests
    > to see whether I could get a directory with *.class files onto the
    > CLASSPATH when starting a JARred Java app (java -jar someApp.jar),
    > using either the JARs Manifest file, java's -classpath option or by
    > setting the CLASSPATH environment variable in a batch file.
    >
    > It appears it can't be done.
    >
    > A) can anyone confirm this?


    Yes. The documentation is pretty clear about this. From the java man page (does
    Windows have an equivalent?):

    -jar Executes a program encapsulated in a JAR archive.
    ....
    When you
    use this option, the JAR file is the source of all
    user classes, and other user class path settings
    are ignored.

    >
    > B) are there any known reasons for this seemingly arbitrary
    > restriction?


    The java command a user enters to execute the jar is not under your control.

    Use the manifest to set the class-path for your jar, that is under your control.


    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
     
    Nigel Wade, Nov 30, 2005
    #9
  10. Lukas

    Guest

    Heard of manuals?

    http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html

    -jar
    Execute a program encapsulated in a JAR file. The first argument is
    the ...
    ... Jar files and Jar-file manifests.
    When you use this option, the JAR file is the source of all user
    classes,
    and OTHER USER CLASS PATH SETTINGS ARE IGNORED.

    Emphasis mine ...

    Kind regards,

    Peter Kriens
     
    , Nov 30, 2005
    #10
  11. Lukas

    Chris Uppal Guest

    wrote:

    > When you use this option, the JAR file is the source of all user
    > classes,
    > and OTHER USER CLASS PATH SETTINGS ARE IGNORED.
    >
    > Emphasis mine ...


    Check the previous clause; that /states/ that application classes are not
    loaded from anywhere except that JAR file -- which is manifestly false. So,
    given that the documentation is wrong, what should the interpretation be ?
    Suppose we conclude that it means that the classpath is ignored; that still
    leaves open the question of whether it is possible to refer to external
    directories (as opposed to JAR files).

    -- chris
     
    Chris Uppal, Nov 30, 2005
    #11
  12. Lukas

    Lukas Guest

    >> The java command a user enters to execute the jar is not under your control.
    >> Use the manifest to set the class-path for your jar, that is under your control.


    Indeed, I preferred using the manifest file to set the classpath and I
    do understand the concept of encapsulation that's enforced there.

    The 'arbitrary restriction' I was referring to is that, when using the
    manifest, you can only put Jars on the classpath, not directories with
    lose class files in them.
     
    Lukas, Nov 30, 2005
    #12
  13. Lukas

    Timbo Guest

    Re: Starting java with -jar option precludes *.class files on CLASSPATH?

    Chris Uppal wrote:
    > wrote:
    >
    >
    >> When you use this option, the JAR file is the source of all user
    >>classes,
    >> and OTHER USER CLASS PATH SETTINGS ARE IGNORED.
    >>
    >>Emphasis mine ...

    >
    >
    > Check the previous clause; that /states/ that application classes are not
    > loaded from anywhere except that JAR file -- which is manifestly false.


    Can you explain what you mean here? I think you are talking about
    loading classes from within your source code?
     
    Timbo, Dec 1, 2005
    #13
  14. Lukas

    Chris Uppal Guest

    Timbo wrote:

    > > Check the previous clause; that /states/ that application classes are
    > > not loaded from anywhere except that JAR file -- which is manifestly
    > > false.

    >
    > Can you explain what you mean here? I think you are talking about
    > loading classes from within your source code?


    I'm not sure what you are asking. But all I meant was that the sentence
    (fragment):

    When you use this option, the JAR file is the source of all user classes,

    is unambiguously claiming that there is no other source of user-defined
    classes, that only classes in /that/ JAR file will be available. Hence the
    application must be written using only the classes that are present in that JAR
    or which are part of the standard platform library. Which is not true (or so I
    believe -- I've never had occasion to test it).

    -- chris
     
    Chris Uppal, Dec 1, 2005
    #14
  15. Lukas

    Timbo Guest

    Re: Starting java with -jar option precludes *.class files on CLASSPATH?

    Chris Uppal wrote:
    > Timbo wrote:
    >
    >
    >>>Check the previous clause; that /states/ that application classes are
    >>>not loaded from anywhere except that JAR file -- which is manifestly
    >>>false.

    >>
    >>Can you explain what you mean here? I think you are talking about
    >>loading classes from within your source code?

    >
    >
    > I'm not sure what you are asking. But all I meant was that the sentence
    > (fragment):
    >
    > When you use this option, the JAR file is the source of all user classes,
    >
    > is unambiguously claiming that there is no other source of user-defined
    > classes, that only classes in /that/ JAR file will be available. Hence the
    > application must be written using only the classes that are present in that JAR
    > or which are part of the standard platform library. Which is not true (or so I
    > believe -- I've never had occasion to test it).
    >

    I *think* this is the case. Where else can the classes come from?
    Certainly if you specify -jar, the classpath is ignored.

    Tim
     
    Timbo, Dec 2, 2005
    #15
  16. Lukas

    Lukas Guest

    > > is unambiguously claiming that there is no other source of user-defined
    > > classes, that only classes in /that/ JAR file will be available. Hence the
    > > application must be written using only the classes that are present in that JAR
    > > or which are part of the standard platform library. Which is not true (or so I
    > > believe -- I've never had occasion to test it).
    > >

    > I *think* this is the case. Where else can the classes come from?
    > Certainly if you specify -jar, the classpath is ignored.


    If you use the line "Class-Path:" within the JAR's manifest, you can
    specify other JARs to put on the classpath
     
    Lukas, Dec 2, 2005
    #16
  17. Lukas

    Guest

    > Check the previous clause; that /states/ that application classes are not
    > loaded from anywhere except that JAR file -- which is manifestly false.

    What "application classes" are not loaded from the JAR file?

    Kind regards,

    Peter Kriens
     
    , Dec 3, 2005
    #17
  18. Lukas

    Roedy Green Guest

    On 3 Dec 2005 03:23:27 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >What "application classes" are not loaded from the JAR file?


    Classes in the ext directory.
    Sun classes (not considered application)
    classes mentioned in the Jar's Class-Path
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 3, 2005
    #18
  19. Lukas

    Timbo Guest

    Re: Starting java with -jar option precludes *.class files on CLASSPATH?

    Lukas wrote:
    >>>is unambiguously claiming that there is no other source of user-defined
    >>>classes, that only classes in /that/ JAR file will be available. Hence the
    >>>application must be written using only the classes that are present in that JAR
    >>>or which are part of the standard platform library. Which is not true (or so I
    >>>believe -- I've never had occasion to test it).
    >>>

    >>
    >>I *think* this is the case. Where else can the classes come from?
    >>Certainly if you specify -jar, the classpath is ignored.

    >
    >
    > If you use the line "Class-Path:" within the JAR's manifest, you can
    > specify other JARs to put on the classpath
    >

    Ah, of course! Thanks for the tip ;-)
     
    Timbo, Dec 7, 2005
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Hanna
    Replies:
    0
    Views:
    1,318
    Phil Hanna
    Jul 1, 2003
  2. Gabe
    Replies:
    3
    Views:
    1,096
  3. cyberco
    Replies:
    4
    Views:
    3,819
    Roedy Green
    Feb 14, 2006
  4. Gabriel Genellina
    Replies:
    2
    Views:
    313
    Gabriel Genellina
    Dec 19, 2003
  5. Gabriel Genellina
    Replies:
    2
    Views:
    305
    Bengt Richter
    Dec 19, 2003
Loading...

Share This Page