Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Creating a file inside of a JAR at runtime

Thread view: 
Sean C. - 04 Mar 2007 15:27 GMT
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
Sean C. - 04 Mar 2007 18:54 GMT
> 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
Lew - 04 Mar 2007 20:40 GMT
> 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
[quoted text clipped - 4 lines]
> 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
Lew - 04 Mar 2007 20:47 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.