Results 1 to 7 of 7
  1. #1
    jimm1 is offline Member
    Join Date
    Jan 2008
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Default Problem loading resources.

    This is a problem that has been bothering me for over a month, and I still can't find a fix, yet this problem is really simple!

    I have a program (In JAR format) that loads resources from a folder (Which is in the same directory as the JAR file).

    I'm using Linux. If I run the program from the shell (java -jar organiser.jar) it runs fine. But now I want to make my program more professional and user friendly, so of course I will assume normal people will double click on the JAR icon. (Normal people DO NOT USE COMMAND LINE). However unfortunately, if I try making a shortcut to the JAR, or run it from the Nautilus browser on Linux, it assumes the working directory is in my home directory, and hence looks for the resources in the wrong place! If I use getClass.getResource() that works fine, however I dont only want to read resources, I also need to write resources, and if I use relative filenames it writes/reads them from the wrong place. I do NOT want to use full pathnames, that will mean I have to have 2 versions for Linux and Windows. Is there some way of bypassing this?

  2. #2
    jimm1 is offline Member
    Join Date
    Jan 2008
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Default

    This problem is keeping me up at night, if I don't fix this, I am going to end it all! My career as a future programmer will be ruined! And I will end up having to study something I'm not interested in.

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default For Windows

    Hello jimm1

    I'm a bit new to Java, but I want to help. For Windows, I would create ".bat" files to do console related tasks that the average user does not understand. Maybe you can do the same in Lunix.

    Good luck though.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    You can keep full paths for both linux and windows inside variables. Then in your application you can check the system (System.getProperty("os.name") and use one of the windows/linux paths only. As far as i undertand your problem, you will not need two different versions of your application in this case.

  5. #5
    jimm1 is offline Member
    Join Date
    Jan 2008
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Default

    Ah thanks, that sounds like the simplest. I know a method for getting the location of the JAR file, but it is complex. I used a shell script (Equivalent to Window's batch file), and that also does work. But unfortunately if I make a link/shortcut to that script, it assumes the wrong working directory again.

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    There are a lot of things in System.java that can save your carrer from ruin and your project from destruction. Be sure to read the whole thing, I will as soon as I can.

    EG: Instead of using \n use System.getProperty("line.terminator"); That may be the wrong call but for example this will prevent several nasty hangs that are highly system dependent.

    Additionally, I have found that proprietary win insists on occasional failure. It is definite that some things are coded such that they will eventually fail. That way the customer has to buy more software.

    The new install does a .reset(); on some hidden gotchas. Be sure to have a second job as manger trainee at some fast food place down the street incase boss donut understan.

  7. #7
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    why don't you create shell file for launching the jar file:

    Java Code:
    #!bin/sh
    java -jar /path/to/the/file.jar
    This way almost all the linux programs work,you can write or shell file either perl file for launching the application,and put it to the /usr/bin or /usr/lib according to what Linux you have
    Last edited by serjant; 06-23-2008 at 08:34 PM.

Similar Threads

  1. Why Resources are needed in Spring FrameWork
    By Java Tip in forum Spring Framework
    Replies: 0
    Last Post: 04-02-2008, 11:40 AM
  2. Problem locating resources on Linux.
    By jimm1 in forum New To Java
    Replies: 1
    Last Post: 01-18-2008, 07:34 PM
  3. Why Resources are needed in Spring FrameWork
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:24 PM
  4. how to check available resources in java program
    By lealea in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 09:35 PM

Posting Permissions

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