aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes copy a property file to the dir where the runme.jar runs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "copy a property file to the dir where the runme.jar runs" Watch "copy a property file to the dir where the runme.jar runs" New topic
Author

copy a property file to the dir where the runme.jar runs

Jorgen Sandstrom
Greenhorn

Joined: Oct 18, 2006
Posts: 8
Hi, just started with URLyBird 1.1.1 - seems to be pretty old, anyway

I have stored default properties files inside the runme.jar file and want to copy e.g 'server.properties' to file location where the jar is located in the filesystem.

'c:\java -jar runme.jar server' - In this case it's no problem, can use String path = System.getProperty("user.dir");

but for the case I got trouble.
'c:\java -jar backup\SCJD\runme.jar server'
Is there a way to get the -jar argument from "inside" java.

BR Jorgen
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Hi Jorgen

Not pretty sure why you want to do that, but you coud use a BufferedReader and PrintWriter to copy the file.



My instructions states clearly, however, that the properties file must be named suncertify.properties and should reside int the execution directory.
Jorgen Sandstrom
Greenhorn

Joined: Oct 18, 2006
Posts: 8
Thanks for the answer.

Yes you are right, must be named suncertify.propertis

My thougt was that ensure that the property file always would be outcopied from the jar to the directory where the runme.jar is started from.

Runned 1st time
c:\scjd java -jar runme.jar server - No prop file can be found in current dir and it will be copied and placed in c:\scjd\suncertfy.properties

2:nd run
c:\java -jar \scjd\runme.jar server - working dir is now "c:\" and the property file from (1) can not be found.

I had the same solution to copy the property file to current dir, but would like to take respect to path entered in -jar argument.

Hope my decription is clear this time.
BR
Jorgen Sandstrom
Greenhorn

Joined: Oct 18, 2006
Posts: 8
Found something intresting in ClassLoader...seems to work

URL url = ClassLoader.getSystemResource("server.properties");
System.out.println("path: " + url.getPath());

C:\>java -jar SCJD\runme.jar server
Starting RMI Server...
path: file:/C:/SCJD/runme.jar!/server.properties

C:\SCJD>java -jar runme.jar server
Starting RMI Server...
path: file:/C:/SCJD/runme.jar!/server.properties

I should now be a pice of cake.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
I must tell you, Jorgen, that although your strategy could work, the most probable is that the settings that you put on the suncertify.properties file will have to be changed by the user every time he runs the application.

For instance, in my suncertify.properties I put the path to the data file, the port number used by server and clients, the default encoding, among other things.

When I start the server, I let the user choose where the file is, what port he wants to use, what 8-bit encoding he wants to apply to the file, etc. Once he aproves the settings he can start server.

Therefore, copying a file from the runme.jar to the local directory might work to set initial values for the settings, if you want to do so, but you cannot assume that the settings in your file are correct.

For instance, the port that you chose to put in the properties file might be used for other purposes on the evaluator machine. The data file could be located in other place, etc.

I tell you all this, because at the begining I was doing the same thing as you are now, and I changed my mind at the end.
[ October 18, 2006: Message edited by: Edwin Dalorzo ]
Jorgen Sandstrom
Greenhorn

Joined: Oct 18, 2006
Posts: 8
Hi again Edwin.

I agree that the outcopied properties file is just for default settings, and should be changed from a GUI before the RMI server is ready for clients.

My initial question was that I always want to keep the runme.jar and the propertyfile in the same directory, independent of path structure in the -jar argument.
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
Why not do the following:

1) Check if there is a properties file in the current directory.
2) If there is, start normally with the values stored in the properties file.
3) If there isn't, create a new properties file in the directory, populated with default values.

This ensures that you never overwrite the user's properties by accident.

J


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Jorgen Sandstrom
Greenhorn

Joined: Oct 18, 2006
Posts: 8
Thanks for the advises, this is how my server works now, maybe I'm starting to do this properties issue too complex. Leave it for now, time to code something more important.

BR Jorgen
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
In my assignment i simply check if there exists the properties file. If not then i show an error message and quit. Most of the applications work like that. After all you need to take a stand as to where to draw the line. If you mess up with the properties then what will can you do? It is a nice to have that you load a default set of properties but then what is the SCOPE?

I added the various UI Strings and labels as well to the properties but then it added a lot more complications. What is the Date Format (picked from props) is incorrect? what do you do? again go to the default properties?

Use the KISS Logic man. Keep it simple stupid!!


-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: copy a property file to the dir where the runme.jar runs