1

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)
  {

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
genx1mx6
  • 425
  • 1
  • 6
  • 12
  • 1
    Was there supposed to be something after *"Here is what I've done. created a new resource file"*? In edit, it appeared you were starting a new list. – Andrew Thompson Oct 29 '12 at 01:29
  • 1) *"I am hoping to distribute my program to others and I'm assuming JAR file isn't the best way."* You're assuming wrong. If it is a rich client (e.g. Swing/AWT/SWT) desktop app., deploy it using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). 2) *""converting to exe" is this good practice? what are the limitations?"* 1 OS out of 3 major platforms seems pretty limiting. 3) Change `catch(Exception e) { }` to `catch(Exception e) { e.printStackTrace(); }` – Andrew Thompson Oct 29 '12 at 01:30
  • The `directories.txt` file is not a "resource", it's an external file. Simple use `File file = new File(`resources/directories.txt`)` to gain a reference and either a `FileReader` or `FileInputStream` to read it. – MadProgrammer Oct 29 '12 at 01:40
  • @MadProgrammer The default properties really ***need*** to be made into a resource of the app., though. – Andrew Thompson Oct 29 '12 at 01:45
  • @AndrewThompson I, personally, have no idea of the requirements. That said, in order to have the resources stored inside the Jar, then it needs to be stored in the `src` location of the Netbeans project. But I agree, better to use a cascading config with defaults defined within the application – MadProgrammer Oct 29 '12 at 01:47
  • @MadProgrammer *"I, personally, have no idea of the requirements."* ..And your comment just reminded that I am presuming more than actually knowing the requirements. Kevin - Insert much more information that clarifies the things discussed by both MadProg & myself. – Andrew Thompson Oct 29 '12 at 01:51
  • @AndrewThompson I think you're idea is right, he may need two configuration files, one with the defaults and one with the user settings. If he wants to write to the file, it can't be embedded within the Jar (not without a lot of really messy work)...IMHO :P – MadProgrammer Oct 29 '12 at 01:54

2 Answers2

5

"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,

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

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/

thedayofcondor
  • 3,860
  • 1
  • 19
  • 28