what is the path of my program?

hi there,

is there anyway to tell what is the path of the program I'm running? (io wise)

this works, but I dont understand the ././././.

public class File4

{

public static void main(String[] args)

throws Exception

{

System.err.println(new File("././././.").getCanonicalPath());

}

}

[349 byte] By [bronze-starDukes] at [2007-11-26 12:12:51]
# 1
String currentDir = System.getProperty("user.dir");But if you write code which depends on the current directory being set to a particular path, you're likely designing things wrong.
platinumsta at 2007-7-7 14:13:21 > top of Java-index,Archived Forums,Socket Programming...
# 2
thanks!
bronzestar at 2007-7-7 14:13:21 > top of Java-index,Archived Forums,Socket Programming...
# 3

Depends what you mean by "path to the program."

Possibilities:

* The path to the JVM that's executing. That (the executable) is usually what's meant by "program" in that context. Not generally available as far as I know. The java.home system property might be useful here, but I'm not sure exactly what it reflects. Not generally useful.

* The path to the main class. Not generally available. You might be able to get it through ClassLoader, URI, File, but the main class doesn't even have to be on your local machine. Not generally useful.

* As warnerja indicated, the user's current directory when he started the VM. user.dir system property. Not generally useful.

It might help if you explained what you're trying to do. Could be you just want to put some resource in your classpath and access it with Class.getResource or getResourceAsStream.

platinumsta at 2007-7-7 14:13:21 > top of Java-index,Archived Forums,Socket Programming...
# 4
well, actually, I have a war file which i deploy on the webapps, and I thought by getting the "path of the program" I'll be able to save and upload files. anyhow, this methodology doesn't work, but if you have some pointers I'll be happy. thanks
bronzestar at 2007-7-7 14:13:21 > top of Java-index,Archived Forums,Socket Programming...
# 5

Just decide where you want to save uploaded files (if that's what you meant by "save and upload files"). That location should normally be outside the web application, in other words in a directory that has absolutely no relation to the WAR file. And then you don't need to know "the path of your program". You just need to know what you decided.

platinumsta at 2007-7-7 14:13:21 > top of Java-index,Archived Forums,Socket Programming...
# 6
If you want upload somethings, you can't use a war file. Instead of, you should use an expanded directory.Use the getRealPath method of ServletContext to get the physical path. http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContext.html
bronzestar at 2007-7-7 14:13:21 > top of Java-index,Archived Forums,Socket Programming...
# 7

so if I understand you correctly you are saying this:

what ever file I want to save it should be saved outside the webapps and if I wish to refer to any saved files, I should upload them from the directory I initially created. So basically, I have to choose a directory and everything goes in and out from there, am I right?

bronzestar at 2007-7-7 14:13:21 > top of Java-index,Archived Forums,Socket Programming...
# 8
Yes.
platinumsta at 2007-7-7 14:13:21 > top of Java-index,Archived Forums,Socket Programming...
# 9
thanks Doctor
bronzestar at 2007-7-7 14:13:21 > top of Java-index,Archived Forums,Socket Programming...
# 10
If you are looking at the relative path of your program then You can use the following to get the path of your program from your root directory.this.getClass().getName();Remember this does not give you the Absolute pathRegards,
bronzestar at 2007-7-7 14:13:21 > top of Java-index,Archived Forums,Socket Programming...