Connecting Tech Pros Worldwide Forums | Help | Site Map

How to refer to the jar filepath relatively?

Newbie
 
Join Date: Jun 2008
Posts: 1
#1: Jun 23 '08
Hey guys,

I'm finishing up on my newest apps and I need to store a file in a location of my .jar. So here's my situation:
I got a jar at C:\

so essentially, in the command line i made this call:
javaw -jar C:\myjar.jar C:\Mydocs\myfile1

I need to store something at C:\ but if I use relative path, the file is stored on C:\Mydocs\ instead. I cannot use absolute path however since when my user install the apps, I couldn't possibly force them to install on a specific location, I want it to be with the .jar.

Any idea on how to do this?
Thanks



Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#2: Jun 27 '08

re: How to refer to the jar filepath relatively?


Greetings, wtver87!

My English is not yet too sophisticated so I did not quite get what you are asking. If you could go into it again it would be helpful and I can tell you an answer or idea would come from all sides...

Also, if you have a pseudocode or outline, please add here for looks. Be sure to fetch results within this forum, something may have been posted.

See you soon!
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#3: Jun 27 '08

re: How to refer to the jar filepath relatively?


Expand|Select|Wrap|Line Numbers
  1. import java.net.*;
  2. import java.security.*;
  3.  
  4. public class FromWhere {
  5.     public static void main(String[] args) {
  6.         Class cls = FromWhere.class;
  7.         ProtectionDomain domain = cls.getProtectionDomain();
  8.         CodeSource source = domain.getCodeSource();
  9.         URL url = source.getLocation();
  10.         System.out.println(url);
  11.     }
  12. }
This code may help. It works if your class file is not jarred or if the class file is in a jar that is in the file system.

An alternative is to use the user's home directory:

String homeDirectory = System.getProperty("user.home");
Reply