I have been playing around with an auto updating client for a project I am working on.
I have been able to get the client to update fine and save a copy of the new client into where ever the original was run from using the below code:
File file;
CodeSource src = HemClientMain.class.getProtectionDomain().getCodeSource();
if (src != null)
{
try
{
URL url = new URL(src.getLocation(), "UpdatedClient.jar");
System.out.println("Writing to: " + url.getPath());
file = new File(url.getPath());
if (!file.exists())
{
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
However when I tried running this on my laptop I get a file not found error:
Writing to: /C:/Documents%20and%20Settings/Rob/Desktop/UpdatedClient.jar
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at Client.ClientGameThread.saveNewClient(ClientGameThread.java:584)
at Client.ClientGameThread.gameClientFinishedUpdating(ClientGameThread.java:564)
at Client.ClientListenThread.dealWithPacket(ClientListenThread.java:96)
at Client.ClientListenThread.listen(ClientListenThread.java:74)
at Client.ClientListenThread.run(ClientListenThread.java:38)
It's clearly something to do with the location and the path not being correct, as if I move the client to say .. c:/ and run it it updates and saves fine.
Is it something to do with the spaces in between document and settings? And if so how do I stop this from happening? Is there another way I can accurately find the path to the current jar file and save a copy there?
I used to do it via this code:
String fileLocation = System.getProperty("java.class.path");
However I now have more than one thing in my classpath and this doesn't produce one path but a combination of them all ... which is not really what I want. There must be a nice way to do this!
Any help / suggestions appreciated. Any further questions I will attempt to answer my best :)