Executable Jar file reading files.

B

Bojan

I am using RAD (Rational Application Developer) to create a java
application. I have multiple folders with java files, and one folder
named Resources with a file header-script.html

In one of the classes I have the following line.

BufferedReader br = new BufferedReader(new FileReader("Resources/
header-script.html"));

With this line, it works fine when I run it in RAD.

but when I export it as jar file (with proper starting main class) it
throws an IOException:
java.io.FileNotFoundException: Resources/header-script.html (The
system cannot find the path specified)

How do I make the executable jar file act the same as when I run it in
RAD.
 
A

Alan Gutierrez

BufferedReader br = new BufferedReader(new FileReader("Resources/
header-script.html"));

With this line, it works fine when I run it in RAD.

but when I export it as jar file (with proper starting main class) it
throws an IOException:
java.io.FileNotFoundException: Resources/header-script.html (The
system cannot find the path specified)

The file is not found. You're loading a file that is relative to the
current working directory of your RAD (IDE). When you deploy your
application in a JAR, those files are still in your working directory.

You need to add the files to the JAR and then use Class.getResource()
to get to them. Class.getResource() is how you get a file as a stream
of bytes out of a JAR (or off of a directory in your CLASSPATH).

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResource(java.lang.String)

Alan Gutierrez - (e-mail address removed) - http://blogometer.com
 
B

Bojan

The file is not found. You're loading a file that is relative to the
current working directory of your RAD (IDE). When you deploy your
application in a JAR, those files are still in your working directory.

You need to add the files to the JAR and then use Class.getResource()
to get to them. Class.getResource() is how you get a file as a stream
of bytes out of a JAR (or off of a directory in your CLASSPATH).

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResou...)

Alan Gutierrez - (e-mail address removed) -http://blogometer.com



I am sorry but I do not understand what you are talking about. The
files are already included in the JAR and it does not work. I do not
understand what u mean by Class.getResource(String resourceName). Can
you please explain with the file header-script.html that is in
Resources folder, that needs be obtained as BufferedReader br in a
class that is in folder JavaClasses.

Thank you
 
R

Roedy Green

How do I make the executable jar file act the same as when I run it in
RAD.

Either:

1. use an absolute fully-qualified filename.

2. make the CWD (current working directory) the same as you had it in
your RAD. It needs to point to the directory where your script is.

Your question is peculiar. It is a newbie question, but you are using
a RAD, something most people don't get involved with until they have a
lot of experience.

For background see http://mindprod.com/jgloss/file.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Danish studies of 10,000 birds killed revealed that almost all died in
collisions with buildings, cars and wires; only 10 were killed by windmills.
Alternative energy sources are absolutely necessary.
Global warming will kill birds and bats, as well as other species,
in much greater numbers than wind power."
~ Dr. David Suzuki
 
L

Lew

Roedy said:
How do I make the executable jar [sic] file act the same as when I run it in
RAD.

Either:

1. use an absolute fully-qualified filename.

2. make the CWD (current working directory) the same as you had it in
your RAD. It needs to point to the directory where your script is.

Your question is peculiar. It is a newbie question, but you are using
a RAD, something most people don't get involved with until they have a
lot of experience.

It's not "a RAD", it's just "RAD", a brand name and acronym for "Rational
Application Developer" (as the OP stated), a commercial version of Eclipse.
It's not that unusual for a newbie to use RAD; often it means they're learning
on the job.
 
A

Arne Vajhøj

Lew said:
Roedy said:
How do I make the executable jar [sic] file act the same as when I
run it in
RAD.

Either:

1. use an absolute fully-qualified filename.

2. make the CWD (current working directory) the same as you had it in
your RAD. It needs to point to the directory where your script is.

Your question is peculiar. It is a newbie question, but you are using
a RAD, something most people don't get involved with until they have a
lot of experience.

It's not "a RAD", it's just "RAD", a brand name and acronym for
"Rational Application Developer" (as the OP stated), a commercial
version of Eclipse. It's not that unusual for a newbie to use RAD; often
it means they're learning on the job.

Roedy could also mean "a RAD tool" as in
http://en.wikipedia.org/wiki/Rapid_application_development !

At least 10-15 years ago RAD was a common marketing label for IDE's
with a WYSIWYG GUI builder.

Based on the context I think the original poster are talking
about the same RAD as you.

Arne
 
A

Arne Vajhøj

Lew said:
The OP's statement
would be a big, fat clue.

Very big and fat.

:)

Roedy did not quote that.

But what he did quote "when I run it in RAD" was sufficient as
small and slim clue.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top