简体   繁体   中英

Using resource files in Java

I'm just getting into using "Java Resource Files" and i have a few questions...

  1. I am hoping to distribute my program to others and I'm assuming JAR file isn't the best way. I'd probably go about it by "converting to exe" is this good practice? what are the limitations?
  2. If I convert to an exe does it keep the resource files?
  3. I'm actually just trying to use a resource file. This file is a text file and will just save the users directories for certain files so they don't need set them up every time they open the program. is this even the best way to go about it?
  4. how do you reference the resource file in the code itself?

Here is what I've done. created a new resource file and since I'm using Netbeans I can see its location under the files tab in the navigator it looks like this:

  • Mainproject
    1. build
    2. classes
    3. myclass
    4. resources
      • directories.txt

here is how i'm trying to access it but when i debug it is coming back null.

private void getPaths()//todo
{   

try
{
 InputStream is = getClass().getResourceAsStream("/resources/directories.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) 
{
    System.out.println(line);
}
br.close();
isr.close();
is.close();
}
 catch(Exception e)
  {

    }
}

"Converting to EXE" is just a fancy way of saying "wrapping the Jar files into an executable container"

"I'm assuming JAR file isn't the best way" not really. It's nice to provide a OS specific means for launching the program at times, but it's not always the best solution.

"what are the limitations?" . Well, to start with, you're limiting your self to a single platform. For Mac's you need to bundle the application into a "app" bundle. For linux, I think most people provide scripts to launch their code.

You could also be limiting your self to particular bit depth. If all you supply is a x32 bit executable, then you'll only ever run within a x32 bit environment. This may not be an issue, but you're limiting the available memory to start with...

So yes, generally, your resource files will be safe.

A resource file is generally embedded. What you're describing in part 3 is more akin to a configuration file. This file needs to be stored on the file system (out side of your exe/jar) so it can easily be updated.

"how do you reference the resource file in the code itself?"

For embedded resources you will need to start by using getClass().getResource(...) . For you configuration file, I'd say just like any other file...

I would also have a look at Deployment some ideas on the suggest mechanisms for deploying Java programs,

Jar is a perfect format for distribution. You can convert to exe , but the user will still need the JVM installed to run it. Jars are executed with a doubleclick if the JVM is installed AND the jar has a properly formed manifest file.

You can open any file from the JVM, text, binary, XML, property file etc.

To save user settings a good choice is a property file - see http://www.mkyong.com/java/java-properties-file-examples/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM