getting jar file's place

G

Gergely Buday

Hi Folks,

I know this is a recurring theme on java groups, but I do ask it
again:

I'd like to get the directory where my .jar file resides. I do know
that it is not always possible, but my users will use the "W"
operating system, and don't download my code from the net, so I expect
my jar file to be in a simple directory.

I have a directory structure

..
demo.bat
demo.jar
img/ -- images
dll/ -- dlls
data/ -- database

so I didn't put everything in the jar file, and I would not like to do
so.

How can I get the full path for "." ?

- Gergely
 
H

Harald Kirsch

I'd like to get the directory where my .jar file resides. I do know
that it is not always possible, but my users will use the "W"
operating system, and don't download my code from the net, so I expect
my jar file to be in a simple directory.

Depending on what you want to do with that directory,

static InputStream java.lang.ClassLoader.getSystemResourceAsStream(String name)

might get you somewhere. The "resource" can be just about
any file anywhere on the class path. As such it is actually
quite similar to a 'new FileInputStream("dada")'.

Harald.
 
O

Oscar kind

Gergely Buday said:
I know this is a recurring theme on java groups, but I do ask it
again:

I'd like to get the directory where my .jar file resides. I do know
that it is not always possible, but my users will use the "W"
operating system, and don't download my code from the net, so I expect
my jar file to be in a simple directory.

You can get the current directory using new File(".").getCanonicalPath(),
and the classpath using System.getProperty("java.class.path") .
Using this information, you can search your classpath for the jar file of
your application.

You can also make your life easier by creating an application jar file,
optionally with "Class-Path" headers in the manifest. When run using
"java -jar jarFile.jar" or "java.exe -jar jarFile.jar", this jar file will
be the onbly entry on the classpath. Note that this is the default on
Windows when the user double-clicks (a shortcut to) the jar file.

I have a directory structure [...]
so I didn't put everything in the jar file, and I would not like to do
so.

How can I get the full path for "." ?

Ok, I wrote too much. This'll do:
String currentDir = new File(".").getCanonicalPath();


Oscar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top