I pack some executable files into the installer, I want to know how can I get application's installation path(Win/Mac). Executable files are under the installation directory.
You can get User working directory (pwd in linux) using this,
String workingDirectory = System.getProperties().getProperty("user.dir");
You can get the .jar file location using this,
URL jarLocation = getClass().getProtectionDomain().getCodeSource().getLocation();
You can put the path into a String like so:
String workingDirectory = System.getProperties().getProperty("user.dir");
Or print it:
System.out.println(System.getProperties().getProperty("user.dir"));
Related
I want to specify the path dynamically. myapp/CopyFolder and myapp/RunFolder's are inside application like myapp/WEB-INF. The code I have given below is in .java file(in eclipse) and in .class file(in tomcat inside myapp/WEB-INF/classname/packagename/). My deployment is in tomcat.
try {
functionNamesObject.Integration(
".txt",
path+"\\CopyFolder",
path+"\\RunFolder",
"app.exe",
"Input.txt"
);
I want path to be dynamic when I call above function. I tried with getResource("MyClass.class") ,new File("").getAbsolutePath(); and System.getProperty("user.dir") but no use. Is there any other way?
You can get the path value as below:
URL resource = getClass().getResource("/");
String path = resource.getPath();
This will return the absolute path to to your myApp/WEB-INF/classes directory.
I tried to reach a special path in Ubuntu, relative to the current jar file.
In Windows it is working without any problem:
String jarPath = Configuration.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File f = new File(jarPath+"/../../configurationFile.xml");
However, in Linux I always get the jar file but I cannot step back two directories to the configurationFile.xml
/some/directory/where/xml/is/located/xyz.jar/configurationFile.xml: Not a directory
However, if I do
pwd /some/directory/where/xml/is/located/xyz.jar/../../
it works without any problems.
What I am doing wrong here?
I cannot figure it out.
Use only directories in your path.
After you determined the path to your jar file, extract the path to its directory and use directories only.
I want to create an installer in java, that copy files from the source (like a packpage to put the files) to the Appdata folder, Is this possible? How can I make this?
String homeDir = System.getProperty("user.home");
String myAppFolderName = ".MyApp";
Path installDir = Paths.get(homeDir, "AppData");
if (!Files.isDirectory(installDir) { // Maybe not Windows
installDir = Paths.get(homeDir);
}
Path myAppFolder = Paths.get(installDir.toString(), myAppFolderName);
Files.createDirectory(myAppFolder);
Path sources = Paths.get(new URI("jar:file://... .jar!/install_image"));
Files.copy(sources, myAppFolder);
For a jar's File, URI:
MyAppClass.class.getProtectionDomain()
.getCodeSource().getLocation().toURI().getPath()
This uses
A fall back whenever there is no AppData directory (as on Linux or Mac)
Some subdirectory .MyApp to put everything in
A zip file system ("jar:file:/...") for the unpacking
A way to get the URI of a jar
You'll probably want to capture the case of running without jar too - for development.
We are working on project.
Every colleague have different folder for the install
In my case the folder of my files is in
C:\\p4_3202\\CAR\\car.rt.appl\\dev\\car.components\\cars\\res\\car.rt.components.cars\\resources\\js;
for other colleague it could be in
C:\\my_3202\\CAR2\\car.rt.appl\\dev\\car.components\\cars\\res\\car.rt.components.cars\\resources\\js;
it is depends how you config your perforce.
I need to read files from my folder but i don't know the name of the folder ( as i explained it could be different )
File folderFile = new File(folder);
How i can find the location of my folder ? ( c:\p4\......test.js )
I tried with
System.getProperty("sun.java.command");
System.getProperty("user.home")
but it didn't give me the path of my folder
I would use a system property for each user. So all users tell where perforce is installed (might already exist a property for this, look at the docs).
This could then be read by your code like:
System.getenv().get("PROP");
On a unix/Linux system you can set the property in a shell/environment variable using:
export PROP=thepath
Windows was a long time ago for me but if I remember correctly its somewhere under System on control panel :)
Update:
http://www.itechtalk.com/thread3595.html
file.getAbsolutePath() or file.getAbsoluteFile()
String path = new java.io.File(".").getCanonicalPath();
If it are files for reading only, being stored inside the final produced jar, then use URL url = getClass().getResource("/.../...") or InputStream in = getClass().getResourceAsStream("/.../...").
That uses a path inside the jar/class path. Using only the jar would never do with File.
I'm trying to get the path to a file that it is located out of the java jar and I don't want to use an absolute path. An exampel: lets say that the jar is located in ~/lib/myjar.jar and the file is located in the same folder. What I've trying is something like this, but it fails:
File myfile = new File(this.getClass().getResource("../../../").toURI());
Note: my package is com.contro.gui, that's why I have "../../../", in order to acces to the "root"
I'm not sure how I can access to the file. Any suggestion?? And what about if the file that I want to access is in other folder like ~/res/ ???
If the file is in the same directory as the jar, I think this will work (feels fairly hacky, but...):
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
File myfile = new File(url.toURI());
File dir = myfile.getParentFile(); // strip off .jar file
(Haven't tested this, but it seems feasible. Will only work with file-based jars of course).
If the file is in some random location, I think you will need to either pass in parameters or check "known" locations like user.home. (Or, you could always put the file in the jar a use getResource().)
No just do
File FileName = new File(".");
String Directory = FileName.getCanonicalPath();
that will get you the parent directory of your class in a file or jar just remember to set the directory if it's in a jar like this "NameOfJar\NameOfFolder\etc"