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());
}
}
# 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.
# 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
# 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.
# 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
# 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?