Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Checking if jars are installed

Options
  • 11-03-2006 2:18am
    #1
    Closed Accounts Posts: 201 ✭✭


    Is there anyway to write in a batch file some code that could check are particular jars are on the classpath of the computer and if they are not put them on it , the jars I am talking about are jars I need for a java program I am writing to function properly but I want to package the jars with the code and have this batch file run on the users computer automatically to see if the jars are already on the classpath and if they are not run the command to place them on the classpath


Comments

  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    Yup...
    erm... sorry,tired...but.. rough outline:

    REM to find out if it/they are there...
    echo %CLASSPATH% pipe output to FIND (whateverthepattern)

    REM if it is.... call :another-part of the batch file (with command to execute)
    (IF ERRORLEVEL... ;) )

    REM and if not.... copy from mapped network share or / set classpath
    (IF ERRORLEVEL... ;) )


    *reads back...i hope that makes sense...It's as good as you'll get from me for now:p


  • Closed Accounts Posts: 201 ✭✭bazcaz


    ya cheers that sorta makes sense Ill have a go at it thanks


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    bazcaz wrote:
    ya cheers that sorta makes sense Ill have a go at it thanks
    If you get stuck - post here / I'll try to help more ...when in a better mental state:p


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    You can do it within java as well. Just don't recall it offhand.. Will have a wander through for an old example.


  • Registered Users Posts: 4,188 ✭✭✭pH


    While all the above is correct you really shouldn't even be trying to do this!

    There are much better options:

    Use Webstart - jnlp files will specify the classpath and download them!
    Put your app in a jar and use a manifest to set the classpath.

    I would strongly advise against ever setting the system CLASSPATH - set the classpath using -cp in the java command in your .bat file.

    You are getting into a complicated mess to do something you shouldn't even be attempting to do - leave the classpath system variable alone!


  • Advertisement
  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    pH is dead right. Assuming that the filename of the jar isn't going to change is dumb: in the stuff I'm working on, two of our jars are going to change their names. This also assumes that you're going to take the stuff from a particular source, which imposes a very strict limit on your app. For example, if you require that a particular Weblogic jar is present, it means that you can't run on Geronimo, Tomcat, Websphere, etc.

    If you really want to check for the presence of something, you should be looking for a class. If you try to access the class inside a try/catch block, you can identify if the class is available. However, this is still silly. If your app needs a particular class, you can assume it's available and fail gracefully on a classnotfoundexception,


  • Closed Accounts Posts: 201 ✭✭bazcaz


    So which way would you recommend I check for the jars presence on the users computer. I have to have some way of checking for their presence because if they are not their my java code will not function correctly.


  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    The simple answer: don't.

    What you really want to do is use the classes inside the jars. So try:
    try {
       FooBar myFooBar = new FooBar();
    } catch (ClassNotFoundException ex) {
       System.out.println("Yoo-Hoo, you forgot your jars");
    }
    

    Of course if your actually installing the app, and want to install the jars along with it, that's quite a different matter. In that case, I suggest Install Shield.


  • Registered Users Posts: 4,188 ✭✭✭pH


    So which way would you recommend I check for the jars presence on the users computer. I have to have some way of checking for their presence because if they are not their my java code will not function correctly.
    That's not what you were asking earlier, you were looking to try and add jars to a classpath, which is completely different from checking if the jars are actually on the computer.

    If this is a real app (not a student project) then Webstart will deliver the app and all its required jars to the user. This is by far the best approach however ...

    If you just want to make sure the classpath is right:
    Put the classpath in the java command line using the -cp switch
    or
    Use a classpath in the jar manifest.

    If you don't want to use webstart then like bp said, getting your app and all its dependent jars to the user is an install problem for which there are a number of tools.

    Finally (and this is what hobbes was probably referring to) although it is notoriously difficult to get the classpath from a running vm, here's an example of java code that will go through the *current* classpath and check that the jars are actually present

    http://www.roseindia.net/javatutorials/checking_classpath_validity.shtml


  • Closed Accounts Posts: 201 ✭✭bazcaz


    I want to check if they are there if they are do nothing if there not add them


  • Advertisement
  • Closed Accounts Posts: 201 ✭✭bazcaz


    pH wrote:
    That's not what you were asking earlier, you were looking to try and add jars to a classpath, which is completely different from checking if the jars are actually on the computer.

    I want to check if they are there first if they are do nothing if they arent add them


  • Registered Users Posts: 4,188 ✭✭✭pH


    Can you try this "thought experiment"

    Forget the "classpath of the computer" even exists, I've been professionally developing java for years and never once used it. There is no need for you to see it, change it or read it.

    When you run a java program you explicitly set the class path (for the code you are about to run) using the -cp switch. (or even better use a manifest in your jar but let's not go there now)

    so you have:
    java -cp my.jar;other-jar1.jar;other-jar2.jar;jar3.jar com.demo.MyClass

    Now explain what exactly you're trying to do


  • Closed Accounts Posts: 201 ✭✭bazcaz


    oh i get ya , so i could use the -cp switch if i have my java code and the jars it needs all inside the same folder(i know they dont necessarily need to be in same folder but this is probably the way I will do it).Would that effect the classpath environment variable on the users computers at all , as in if the user already had the jars on its classpath and I used the copy of the jars that I had packaged and using the -cp switch to refer to them would there be any conflict??

    Thanks for the help again!


  • Registered Users Posts: 4,188 ✭✭✭pH


    No
    - if you specify a classpath using -cp then it is used, anything set in the classpath environment variable is ignored (and not altered in any way either)


  • Closed Accounts Posts: 201 ✭✭bazcaz


    ahh thats a much easier way of doing it then cheers!


  • Registered Users Posts: 4,188 ✭✭✭pH


    You should consider putting all your classes in a jar and then adding a manifest that specifies the classpath and the main class.

    then it is run using

    java -jar my.jar

    http://java.sun.com/docs/books/tutorial/deployment/jar/


  • Closed Accounts Posts: 26 Selket


    use cmd to check if your jars are there.
    Windows:
    Open cmd and type: set
    Unix:
    Open cmd and type: env
    This will display all the environment variables

    If you need to set them, I recommend that you only set them temporarily, because if you start playing around and you don't know what you are doing you change something you didn't mean too. Also, it appears your using someone elses computer and they won't thank you for messing with their system!

    First I recommend you add a simple variable eg:
    Windows:
    set MYVARIABLE="This is a TEST"
    Unix:
    export MYVARIABLE=This is a TEST"
    The variable is now set and available if you ask for it

    PATH, CLASSPATH etc are ordered lists which tell programs where to look for resources. The lists are delimited by a colon ":" in Unix and a semicolon ";" in windows. I highly doubt that you want to reassign their values, however, if you do want to alter your classpath or path, just append them.
    Windows:
    set CLASSPATH=%CLASSPATH%;C:\Path\to\name.jar
    Unix:
    export
    CLASSPATH=$CLASSPATH:/Path/to/name.jar

    Again, this is only a temporary fix. Once you close the cmd - the new settings are gone. I hope this helps and I'm sorry if I've overstated things, it might help somebody else who is looking for jars or wish to set their classpaths


Advertisement