473,770 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a file inside of a JAR at runtime

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().getC lassLoader().ge tResource (".").toStri ng ();

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
NullPointerExce ption 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
3 4500
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6127
by: owen | last post by:
Generally speaking, what does it mean when I see a "button" with red text showing this message instead of the control I've dragged onto the web form in Design View.? (But the page works fine at runtime). Does it indicate a problem with References for example? Thanks Owen PS. replies cc'd by email appreciated. owen.southwood@mantix.com
4
1764
by: Bernie Yaeger | last post by:
I now know how to gather the file type icons and I'm able to use them in a listview. But a listbox does not have a .smallimagelist or .largeimagelist member, so I don't know how to translate that so that I have the icon + the string in a listbox. Is this possible, and if so, can you give me some idea how to go about it. Thanks for any help. Bernie Yaeger
1
3849
by: D. Yates | last post by:
Hi, I am looking for an example of how to extract bitmap images from an embedded resource file (a file with *.res extension, which can be viewed inside of the ide and can hold bitmaps, icons, string tables, etc.) and place them into a imagelist. I have found examples using the resource manager to create a "resource file" like so: ResourceWriter rw = new ResourceWriter (
15
6750
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use javascript or vbscript it works. I will appreciate any help. I do the following (C#):
1
1987
by: MT | last post by:
We have a .NET dll that we are deploying to our customers using a deployment project in VS .NET 2003. There is a lot of information about creating publisher policies on the web, but we have not been successful in creating one. Here's what the ultimate goal is: When the DLL is deployed to a customer's system, every application using this dll must point to the new version. This has to be an automated process using the deployment project....
12
3175
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without restarting the application. What I did was to create an AppDomain that loaded the plugins and everything was great, until I tried to pass something else that strings between the domains...
5
3502
by: Sam777 | last post by:
I was under the impression that creating the app_offline.htm file at the root of the webapp would cause all handles to be closed so that the app could be removed. Unfortunately, this isn't the case. One handle remains open. Debugging shows that it's actually the IIS cache and not ASP.NET that owns this handle. During IIS shutdown, the handle is closed with the following stack trace: ChildEBP RetAddr 0006fbe4 5a403e05...
1
3878
by: Luurs | last post by:
Hi All, I've been using a asp-label in the HTML <headsection for quite some time in order to dynamically generate meta-keywords,-description, css and js references. Though the actual HTML-output isn't that pretty (<span>'s in the head-section...) it works fine with browsers, crawlers. It never REALLY was an issue...
6
1782
by: Charlie Bear | last post by:
i'm really stuck with this one can anyone help! i have a website that uses c#. it creates a series of directories and files from an xml source. when the xml changes, the directory that the change applies to is deleted and recreated. All the children for that directory are also re created (as the parent directory then has a different name). the problem i have is that *sometimes* the files or directories don't get created. It either...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.