Results 1 to 5 of 5
  1. #1
    aztectrev is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default get the size and filename of the jar file run from and SHA

    i'm currently writing a client-server rpg game and i used some networking code to allow the program to download its own updates.

    at the minute, whilst im testing it, i want to check the filesize of the jar file compared to the one on the server. how can i get the file size and name of the jar file being run from?

    cant seem to find an easy way to get the jar file name, or the filesize.

    also later on will be using SHA to compute the value of the jar file to check if has been altered, if anyone knows how to do this.

    thanks
    Last edited by aztectrev; 04-03-2010 at 08:45 PM.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Java Code:
    java.net.URL url = this.getClass().getResource("");      
    System.out.println(url.getPath());

    If you run it as a jar it will contain the path to the jar followed by the path of the class in the jar.

    c:/some/where/is/MyJar.jar!/com/myproject/main.class

    You can parse out the file path and use the File object to get details about the file (i.e. file size)

    For the file updater i wrote i used the check sum of the file contents to determine it's version and whether or not it needed updating.

    Java Code:
    CheckedInputStream cis = new CheckedInputStream(is, new Adler32());

    I'm not sure how you would determine if the file is altered or not... they could just send you the value you want to see instead the actual value you would have computed. I would say let them alter the jar if they want... just never trust the client and what they are sending you.
    My Hobby Project: LegacyClone

  3. #3
    aztectrev is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default still a problem

    i use eclipse as my java ide and use the export runnable jar option, but it exports it in a way that the path returned by the URL is 'rsrc:' followed by the package name and not the full path. this seems to be something to do with the jar in jar loader it uses, as it packages the jar libraries.

    the checkedinputstream looks like it could do the job though, once i sort this first problem. another reason i want to use checkedinputstream is the game may change alot and the filesize might be the same, but the contents different. i dont want people to be able to use an old version

    thanks for reply

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    I usually use Netbeans to build my jars but i ran a quick test using eclipse exporting as a runnable jar; eclipse showed my path to be (in linux; ran from console):

    file:/home/someone/Desktop/someone.jar!/Main.class

    another reason i want to use checkedinputstream is the game may change alot and the filesize might be the same, but the contents different.

    Yep seems there would be a pretty high chance of the file size being the same at some point. One issue i found with the checkedInputStream and jar files is the timestamps of the files inside the jar. If the timestamp changes but the file is the same you get a different checksum value. Instead of taking the checksum of the jar i had to take a checksum of each file contained in the jar.
    Last edited by mrmatt1111; 04-03-2010 at 07:19 PM.
    My Hobby Project: LegacyClone

  5. #5
    aztectrev is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default fixed, kind of

    i managed to work around it, it was a prob with exporting, as the external jars were placed within my main jar. i changed the export option to just place them external jars in the same directory, as my own and it now returns proper path and then i could use Checkedinputstream on my jar file

Similar Threads

  1. PDF file size
    By tim in forum Suggestions & Feedback
    Replies: 2
    Last Post: 04-08-2010, 01:59 PM
  2. validation on file size
    By kirtichopra2003 in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2009, 08:08 AM
  3. Jar resource file size
    By OrangeDog in forum Java Applets
    Replies: 1
    Last Post: 04-30-2009, 05:12 AM
  4. Set the Size of jvm.log file
    By kasidandu in forum New To Java
    Replies: 0
    Last Post: 03-16-2009, 05:08 PM
  5. File size
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-19-2007, 10:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •