By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

Creating a file inside of a JAR at runtime

P: n/a
Hey All,

I'm having a little problem here. I have a project that I'm working on
that involves a MySQL server database backend. I'm having no problem
creating the database on the fly if it doesn't already exist and using it
once it's created.

My problem comes into play when I run the program for the first time. I'm
wanting to have some kind of flag that lets me know that the database has
not yet been created, so that I can call my database creation routine. I
started coding this project in a text editor and using the commandline
tools to compile and run the application and didn't have any problem. I
simply used:

TruePath = getClass().getClassLoader().getResource (".").toString ();

to get the path to my main class. However, my text editor started wigging
out on me as the project got larger and larger, and I figured out that I'm
an idiot and can't create the distributable JAR file on my without
corrupting my classes for some reason, so I've moved the project into
NetBeans IDE.

Since NetBeans puts the project into a JAR by default, I've run into
problems with my database creation flag. Now, that line of code I placed
above no longer works as expected. Instead, I keep getting
NullPointerException errors. I've looked through the JavaDocs and have
searched online for a solution to this dilemma, but have not been able to
find anything. I may, of course, be using the wrong search keywords on
Google and just not getting the results that I am looking for, but who
knows.

Does anyone here have any idea as to how I could go about either a)
placing a file (my db creation flag) into my JAR file at runtime, or b)
another idea that I could try for determining if the database already
exists or needs to be created? Any help on this is greatly appreciated.

Cheers,

Sean
Mar 4 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sun, 04 Mar 2007 15:27:42 +0000, Sean C. wrote:
Hey All,

I'm having a little problem here. I have a project that I'm working on
that involves a MySQL server database backend. I'm having no problem
creating the database on the fly if it doesn't already exist and using it
once it's created.
OK, I told you that I was quite possibly being an idiot. I found my own
solution...

I've revamped my code to trying to use the database with the SQL 'USE'
statement and catching the SQLException error in the event the database
does not exist. In my 'catch ( SQLException sqlE )' block, I've called
the routine to create the database. I've tested it a few times and, lo
and behold, success! This works just fine.

Now I have no need to worry about trying to create a file in the JAR at
runtime. Sorry to have wasted your time with this stupid question.

Cheers,

Sean
Mar 4 '07 #2

P: n/a
Lew
Sean C. wrote:
I've revamped my code to trying to use the database with the SQL 'USE'
statement and catching the SQLException error in the event the database
does not exist. In my 'catch ( SQLException sqlE )' block, I've called
the routine to create the database. I've tested it a few times and, lo
and behold, success! This works just fine.

Now I have no need to worry about trying to create a file in the JAR at
runtime. Sorry to have wasted your time with this stupid question.
The solution you found is correct because it checks directly for the condition
of interest, instead of looking for some unreliable side-effect of that condition.

You cannot modify the contents of the running JAR at runtime. When you got the
current "directory" of the program that ran from the JAR, you didn't get a
path to a real directory. I don't even know what meaning one would expect for
"." in the context of a program run from a JAR. A JAR is not a file system.

Instead you wrote your code to determine whether something exists in the
database by asking the database. That just makes sense.

-- Lew
Mar 4 '07 #3

P: n/a
Lew
Sean C. wrote:
I've revamped my code to trying to use the database with the SQL 'USE'
statement and catching the SQLException error in the event the database
does not exist. In my 'catch ( SQLException sqlE )' block, I've called
the routine to create the database. I've tested it a few times and, lo
and behold, success! This works just fine.
You are using an Exception. Is the absence of the database an exceptional
condition?

If "database absent" is an Exception, the expectation is that the database
will be there. If the 'USE' statement is necessary to your logic, then it
makes sense to have it there. If not, then the SQLException will be thrown by
whatever the first statement is that tries to hit the absent resource. There
is no single point of failure; every statement that tries to use the resource
has to check for exceptions. That is true for all database calls anyway, so it
doesn't represent additional overhead.

It is also possible to model "database absent" as a normal condition, in which
case you should not throw an Exception or depend on catching one, but directly
read the catalog to determine if the database exists. Instead of a
SQLException, you'd receive an empty result set or zero count or something
like that.

Choose the idiom that most closely matches the conceptual model of your
business logic.

-- Lew
Mar 4 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.