0

I am trying to make my main executable jar file copy itself to another location.

How can I do this?

Tharwen
  • 3,057
  • 2
  • 24
  • 36
user1947236
  • 673
  • 3
  • 12
  • 27
  • 1
    Does it know from an external source where it is, or does it have to find out itself? Also, can you explain in a few words what you're using this for? – us2012 Jan 09 '13 at 03:17
  • @us2012 No, it does not know where it is. Its kind of hard to explain what this is for, but basically on windows I need to copy it to appdata and on mac to library. – user1947236 Jan 09 '13 at 03:20
  • What is the problem? Do you have difficulties in how to copy with Java or how to find the jar-location? If the jar would copy a different jar - what would be the difference? How is the Program in the jar started? – user unknown Jan 09 '13 at 03:25
  • @userunknown I know how to copy the file, I need help getting its location. – user1947236 Jan 09 '13 at 03:27
  • This article should help you: http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file – Archer Jan 09 '13 at 03:31
  • @archer Would that also get the name of the running jar file? – user1947236 Jan 09 '13 at 03:32
  • You should adjust the headline of your question to its goal. – user unknown Jan 09 '13 at 03:48

1 Answers1

2
return new File(MyClass.class.getProtectionDomain().
 getCodeSource().
 getLocation().
 getURI().
 getPath()).getName();

will return a full name

Archer
  • 5,073
  • 8
  • 50
  • 96