Application Path

R

Roman

Hello,

could someone pleas tell me, how I find out from inside the program, in
what path it is installed?

The property user.dir returns only the working directory but this is not
necessary the path to the Application.

System.getenv(String) is deprecated.

Thank you for any answers in advance.
 
J

Jean-Francois Briere

Try:
System.getProperty("java.home");
(then you could append bin/java -or- bin\java.exe -or- bin\javaw.exe)

Regards
 
R

Roman

David said:
Could you post the code please?

Here a stand-alone example:


import java.net.URL;

public class App {
public static void main(String[] args) {
App app = new App();
System.out.println(
(app.getClass().getResource("App.class")).getPath()
);
}
}
 
R

Roman

Roman said:
David said:
Could you post the code please?


Here a stand-alone example:


import java.net.URL;

public class App {
public static void main(String[] args) {
App app = new App();
System.out.println(
(app.getClass().getResource("App.class")).getPath()
);
}
}

Of course you could replace "App.class" by

app.getClass().getName() + ".class"
 
C

Chris Smith

Roman said:
Here a stand-alone example:


import java.net.URL;

public class App {
public static void main(String[] args) {
App app = new App();
System.out.println(
(app.getClass().getResource("App.class")).getPath()
);
}
}

This code doesn't work. Or rather, it only works if your application is
composed of loose class files, but obviously isn't (or, at least,
hopefully isn't!) the case when you deploy the application.

I wrote a very complete and detailed answer to this question some time
ago. It's temporarily available in Word format at the following rather
ugly URL:

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Roedy Green said:
IF you want I could format that as HTML and with an attribution add it
as http://mindprod.com/jgloss/intrinsicdata.html

In this case I have to say no. I wrote that on my employer's time, and
our plan was to collect a lot of similar documents, convert them to PDF
in a similar format, and place them on MindIQ's web site to lure Java
programmers' search engines in hopes that they'd see our commercial
training courses. That plan isn't actually progressing, but I can't
just give away content that technically belongs to MindIQ.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

David Segall

Roman said:
David said:
Could you post the code please?

Here a stand-alone example:


import java.net.URL;

public class App {
public static void main(String[] args) {
App app = new App();
System.out.println(
(app.getClass().getResource("App.class")).getPath()
);
}
}
Thanks Roman. On my system that returns
"file:/C:/Program%20Files/App/App.jar!/App.class". I was hoping for
something that needed less post-processing to yield
"C:program Files/App".
 
J

Jean-Francois Briere

app.getClass().getResource("App.class")
This code doesn't work. Or rather, it only works if your application is
composed of loose class files

Actually the code works for either loose class files or class files
within jar files.

Regards
 
D

David Segall

Chris Smith said:
Roman said:
Here a stand-alone example:


import java.net.URL;

public class App {
public static void main(String[] args) {
App app = new App();
System.out.println(
(app.getClass().getResource("App.class")).getPath()
);
}
}

This code doesn't work. Or rather, it only works if your application is
composed of loose class files, but obviously isn't (or, at least,
hopefully isn't!) the case when you deploy the application.
I wrote a very complete and detailed answer to this question some time
ago. It's temporarily available in Word format at the following rather
ugly URL:
Thank you for the code in technique #5 which returns the appropriately
formatted directory name that I would expect.

Could you explain why you say using .getResource() does not work? The
raw result seems similar to that obtained from
getProtectionDomain().getCodeSource() and I don't see why some similar
post-processing would not yield the same directory name.
 
C

Chris Smith

David Segall said:
Could you explain why you say using .getResource() does not work? The
raw result seems similar to that obtained from
getProtectionDomain().getCodeSource() and I don't see why some similar
post-processing would not yield the same directory name.

Well, for one thing, getResource() is not guaranteed to work on a class
file in the first place. But in practice, it does work; so that's not
the most important problem.

The real problem is that the original code just doesn't work for code
that's packaged in a JAR file. getResource() returns a URL with the
"jar" scheme, and calling "getPath" on that URL doesn't do what you
want. getCodeSource(), on the other hand, will return a file-scheme URL
pointing to the JAR file itself. That is what you want.

getCodeSource() is also more convenient even for loose classes, if they
are in packages. getResource() returns a path that points deep into the
package structure. getCodeSource() points to the root of that
structure, which is almost certainly what you wanted in the first place.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

David Segall

Chris Smith said:
Well, for one thing, getResource() is not guaranteed to work on a class
file in the first place. But in practice, it does work; so that's not
the most important problem.

The real problem is that the original code just doesn't work for code
that's packaged in a JAR file. getResource() returns a URL with the
"jar" scheme, and calling "getPath" on that URL doesn't do what you
want. getCodeSource(), on the other hand, will return a file-scheme URL
pointing to the JAR file itself. That is what you want.

getCodeSource() is also more convenient even for loose classes, if they
are in packages. getResource() returns a path that points deep into the
package structure. getCodeSource() points to the root of that
structure, which is almost certainly what you wanted in the first place.
Thanks Chris. I'm convinced. I think it would be worthwhile to add
this explanation to your essay since getResource() is most frequently
proposed as the solution to the problem.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top