Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: How to include .ser file into .jar file

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to include .ser file into .jar file

    I've been programming a TETRIS game in JAVA, and i just finished it..
    But when i was trying to execute the .JAR file, i noticed that i couldnt open the only .ser file i have, which is a serializable class containing the 5 highest scores obtained by different players..

    I thought that it was enough to open the file from the application, as if it where an image, or a sound effect, so i put all those resources in the same folder, which i can access properly, but not to read the .ser file. Of course the problem im having is that i cant access it when i execute the .jar file, but it works perfect when i run the application from NetBeans.
    I think now i have to do something with the manifiest, or maybe not.
    Have no idea how to make it work... any suggestions pleaseeee?


  2. #2
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: How to include .ser file into .jar file

    Usually when I have this problem reading files from JARs, it is a character case issue. When you're reading files from disk in windows, the file names are case insensitive. But when you're getting the file as a resource out of a JAR, the file names are case sensitive.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to include .ser file into .jar file

    You should also try to use the class loader getResource methods to read resources from within the JAR file, how do you currently try to open the file?

    // Json

  4. The Following User Says Thank You to Json For This Useful Post:

    yuki (April 8th, 2010)

  5. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to include .ser file into .jar file

    Quote Originally Posted by Json View Post
    You should also try to use the class loader getResource methods to read resources from within the JAR file, how do you currently try to open the file?

    // Json

    Yes actually im opening it using the getResource method, and its working perfect for images and audio.

    URL uRL = GamePanel.class.getResource("/resources/scoreTable.ser");
    if (existsFile(uRL.getFile())) {
                fis = new FileInputStream(uRL.getFile());
                in = new ObjectInputStream(fis);
                scoreTable = (ScoreTable) in.readObject();
                in.close();
                return scoreTable;
    }

    At first i opened it as a File, but when i realized that woudln't work even for images, i change everything and did it that way. That's my problem. I just thought it would work out as it did with other common files.
    So i dont know if the problem is that you always have to do something to open .ser files when running from .JAR, or if im doing something wrong.
    Last edited by Json; April 9th, 2010 at 09:03 AM.

  6. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to include .ser file into .jar file

    Quote Originally Posted by Bent View Post
    Usually when I have this problem reading files from JARs, it is a character case issue. When you're reading files from disk in windows, the file names are case insensitive. But when you're getting the file as a resource out of a JAR, the file names are case sensitive.
    Sorry, but i dont really understand what should i do then.. I just replied to another answer explaining a bit more how im doing it. I dont think its a problem with the file name, nor with the path, cause i can open images from the same folder with no problem at all..
    Thanx for the anwer.

  7. #6
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: How to include .ser file into .jar file

    You'll probably want to use the getResourceAsStream method instead of getResource. If I understand it correctly, the file in the JAR doesn't represent a proper file for a File object, so trying to derive a stream from a file derived from the URL that you get from getResource doesn't work.

    Try something like the following:

    in = new ObjectInputStream(GamePanel.class.getResourceAsStr eam("/resources/scoreTable.ser"))

    Also, the path to the resource in the JAR file should be /resources/scoreTable.ser (with exactly the same string casing! That always gets me!)

  8. The Following 2 Users Say Thank You to Bent For This Useful Post:

    Json (April 9th, 2010), yuki (April 12th, 2010)

  9. #7
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to include .ser file into .jar file

    Quote Originally Posted by Bent View Post
    You'll probably want to use the getResourceAsStream method instead of getResource. If I understand it correctly, the file in the JAR doesn't represent a proper file for a File object, so trying to derive a stream from a file derived from the URL that you get from getResource doesn't work.

    Try something like the following:

    in = new ObjectInputStream(GamePanel.class.getResourceAsStr eam("/resources/scoreTable.ser"))

    Also, the path to the resource in the JAR file should be /resources/scoreTable.ser (with exactly the same string casing! That always gets me!)

    Thanx a lot for your answer.. i just tried that and it seems to work out fine!.. i used the ObjectInputStream, like u said.. the only thing i must do is run the JAR file with the "lib" folder next to it. Otherwise it wont work..

    Ill read a bit about that to make sure i understand why its working, but u helped a lot!
    never thought i could figure it out..haha..

  10. #8
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to include .ser file into .jar file

    if you wanna think with me a bit more, ill tell you that now i must solve another problem, which is writing to, or modifying the .ser file..
    Im not really working hard on this, cause ive been pretty busy lately, but i will try to figure that out..
    of course the getResourceAsStream wont work, cause that returns an Input Stream, and i would be needing an Output Stream...
    thankx again for your help.. its trully appreciated..


  11. #9
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: How to include .ser file into .jar file

    You've got a lot more difficulty there. From what I understand, JAR files were not intended to be dynamic resource caches, but static ones. There are probably a number of issues but one was that JARs were intended to be a means of securely packaging data via JAR signing, and if the application were to modify the archive it would invalidate the signature.

    There may be a third-party library out there that eases this task. Otherwise, you have to manipulate the JAR directly using the utilities in the java.util.zip.* package.

    BTW: JAR's are generally for easy packaging and transport of code and resources. You probably don't want or need to store user preferences or object state data in them. You can easily store your state data in a file or directly directly on the file system next to the JAR file, if you wish them to be near one another. If your program writes to the JAR file, you will have a difficult time maintaining a 'clean' copy of your source code and resources.

  12. #10
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to include .ser file into .jar file

    I understand what u're saying.. maybe im trying to do something easy a lot more difficult..
    its not that i need the file to be into the JAR file, i just need to read the data it contains to show high scores, and update with new scores if they are higher..
    And of course not only in my pc, but anywhere i want, only moving the JAR file and the lib folder..
    what's that easy way to put it next to the JAR file? just opening it as a file, with the same path as the JAR, and for example in a folder /resources/ ??.. i thought that wouldnt work...
    thanx
    Sorry to bother you..

  13. #11
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: How to include .ser file into .jar file

    As a general rule: If it is in a JAR, it is not a File. You have to use the Class.getResourceAsStream methods to get at it.

    If it is a file, (a REAL file, on your file system, NOT in the JAR) you should be able to open it using the Class.getResource('/resources/???') method, just like you do in NetBeans.

    For example, if your directory structure is:

    C:\myApp\myApp.jar
    C:\myApp\resources\myData.ser

    and your application is running from myApp.jar, then you should be able to get the file reference from your program with MyClass.getResource('/resources/myData.ser').

  14. #12
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to include .ser file into .jar file

    yes, thats what i tried to do the first time.. but its not working..
    ill see what i can do, and of course tell you if theres any solution!

    thanks 4 your time..

  15. #13
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: How to include .ser file into .jar file

    Well, to get at the problem, we need a get a fix on more details.
    How are you running the JAR?
    What is your classpath when you run it?
    How are you setting the classpath?
    What is your folder structure?
    What is your code that you are using to access the file?

  16. #14
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to include .ser file into .jar file

    Quote Originally Posted by Bent View Post
    Well, to get at the problem, we need a get a fix on more details.
    How are you running the JAR?
    What is your classpath when you run it?
    How are you setting the classpath?
    What is your folder structure?
    What is your code that you are using to access the file?
    Im running the JAR both from console, and double-clicking on it (double clic to JAR stored in "dist" folder, after compiling from netBeans). When you said i should put the /resources/ folder next to it, i copied the one im storing next to the C:/TETRIS/src/tetris folder, which ill explain below.

    My files are organized as netBeans puts them when you create a project. this is:
    A Folder called TETRIS, which contains all the project, inside there are 5 folders:

    C:/TETRIS/build
    C:/TETRIS/dist -->the moment i compile, it contains the JAR file and the LIB folder.
    C:/TETRIS/nbproject
    C:/TETRIS/test
    C:/TETRIS/src ---> this one contains the "tetris" folder generated by netBeans, and the famous
    /RESOURCES/ folder, with images, audio and .SER file, which is accessed
    trough the getResource method, relative to the GamePanel class, located inside this
    tetris folder.

    All classes are stored in C:/TETRIS/src/tetris/ so when i use ANYClass.getResource(...), is relative to that path. (maybe theres another way to do this, or to set the ClassPath, as you say.)
    Finally, me exact piece of code for writing the new Score Table (when i need to update it) is:

    URL uRL = GamePanel.class.getResource("/resources/scoreTable.ser");
    FileOutputStream fos = null;
    ObjectOutputStream out = null;
    fos = new FileOutputStream(uRL.getFile());
    out = new ObjectOutputStream(fos);
    out.writeObject(scoreTable);
    out.close();

    this code works perfect from netBeans!
    Anything else?

  17. #15
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: How to include .ser file into .jar file

    I did some testing to check it out further. The results were interesting.

    class.getResource returns a a different type of URL depending on whether you're running from a JAR or from a *.class file. When running from a JAR, you get an opaque URL with a scheme of 'rsrc:'. When running from a *.class file, it returns a file URL (a scheme of 'file:'). The 'rsrc' scheme isn't resolved by the FILE class, hence you cannot open a file from it directly.

    However, when running from a JAR file and looking for a directory adjacent to the JAR file, you can use the classpath to find it. If you query System.getProperty("java.class.path") in your class, you should get a path to your running JAR relative to the user directory (System.getProperty("user.dir"), which should return the directory your application is running from.) Note that the java.class.path can return multiple paths separated by semicolons, so you might have to search to find the right one.

    You can combine the classpath to the running jar (minus the JAR file name itself) with the path to your resource and submit it to new File() to get your file.

    NOTE: There may be other ways of doing this by setting the classpath in your JAR or on the command line, but I didn't find any.

  18. The Following User Says Thank You to Bent For This Useful Post:

    yuki (April 19th, 2010)

  19. #16
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to include .ser file into .jar file

    hey!!.. sorry i took too long,. i been a little busy this weekend.
    this last answer has been the solution i think!.. i guess it was what ive been trying to do from the beginning, even before using the getResource() method.
    I used the System.getProperty("user.dir") only.. that gives me the path where the JAR file is located. then if i can keep my resources next to it, problem solved!.
    ill tell you if i have any trouble, but it seems to be working great.
    THANX for everything. its the first time i create a thread, and i thought i wouldnt find any answer.
    anything you need, just ask. ill try my best to help you.

  20. #17
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: How to include .ser file into .jar file

    You need to be careful just using user.dir. The user dir can change depending on how the jar is run. If you run from the command line in the jar directory, for example, using java -jar myApp.jar, user.dir will point the to correct location. But if you're in another directory, it will not.

    For example, if your application path is:

    C:\myAppDir\myApp.jar

    and you invoke

    java -jar myAppDir\myApp.jar

    from the root directory (C:\), user.dir will be C:\

    However, if you look in your java.class.path in this case, you'll see a 'myAppDir\myApp.jar'.

    So, in general, it appears that if you can find the path to your jar in the classpath (which appears to be relative to user.dir) then use user.dir as the path parent, you should be able to get your file in the general case.

    BTW: Note that in typical use case, persisting changeable settings in applications is usually done elsewhere. In particular, for properties, the user.home directory is probably better (typically I use a path off the user.home directory that follows the package naming for my application. Alternatively, for a cross-platform solution, the Preferences API is easy and straightforward to use. On Windows, it will store the data in the Registry, which strikes me as unpleasant, but it is easy to use.) The advantage of storing your changeable settings elsewhere is that you don't get user data being accidently transfered when you're copying your application to another system.

  21. #18
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to include .ser file into .jar file

    yes i noticed that.. i did some testing, printing out the path, and i made it work for the particular case when you run it by double-clicking on the jar file.
    Its not working from netBeans, but no problem. ill then try out with the classpath, but this one works in different computers for sure, and i put the jar next to the lib folder, which i need, and inside this one i created the resources folder containing only de .ser file.
    Thanx again!.. ive learned a few new things.. its a pity we cant know them all! haha.

Similar Threads

  1. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  2. How to change File Type of a File Using java
    By akash169 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: March 31st, 2010, 02:58 AM
  3. end of file \ file offset
    By JayRay in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: March 27th, 2010, 04:11 PM
  4. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM

Tags for this Thread