Copying files from source to computer - java

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.

Related

Get relative path from jar file

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.

How can i get application's installation path in Java?

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"));

Listing files in project directory in Linux environment using File.listFiles() in java

String path = ".";
String files="";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
System.out.println("files::::::::::::"+files);
}
}
Say the above java file I saved under the path D:/spike/FileList.java.
In the above code if I run in windows platform, it will list the files under the 'spike' directory.
But when I keep this in Linux environment say the path is usr/local/apache/webapps/webtest/src/FileList.java
The result I get is files under root directory.
What I require is under the project root folder, i.e. in above case under the webtest directory.
How can I do the same. My requirement is I need to first list the files and then from the list of files I need to read sample.properties file.
I know we can hard code the path to get the same. But without hard coding how can I get the list of files under the project root folder of webapps i.e, under webtest folder in my case.
I also tried by reading the environmental variables. But the problem here is my apache folder owner is spike and not root. So when I execute System.getenv() what I get is only whatever variables the user spike has set.
But when I execute System.getenv() from any other folder whose owner is root, then I get the complete environmental variables.
So is there any way I can get the project root folder by using the above java code snippet without hard coding the path?
By the way this is a web application deployed in tomcat. First the app will read the details from the server.properties file. But Im not supposed to hard code the path as the path changes from system to system. So my intention is that the code read the properties file from the project starting folder.
This is not true, your code on Linux
prints the files in the current folder.
I just tried it.
As to tomcat, see here
What determines the current working directory of Tomcat Java process?
and/or look for similar information on the Tomcat site.
If your file sample.properties is located in the root folder, and actually is a properties-file I would try this loading the file and printing the content:
String filename = "sample.properties";
Properties properties = new Properties();
properties.load(getClass().getClassLoader().getResource(filename).openStream());
properties.list(System.out);
getClass.getClassLoader() will get the location of the rootfolder. If you skip getClassLoader you will get the folder of the package in your class.
(If you are running this in a standalone java app the properties file needs to be located in the classes folder.)
You can try with the current working directory:
String path = System.getProperty("user.dir");
But this depends where exactly is the CWD, e.g. how was this application started?
In the context of a web application, you can do (as long as you have the ServletContext object):
ServletContext application = ...
String path = application.getRealPath("/");
This will give you the root of the web application, you can navigate to the required directory from there.

How can I save a file with a generic file path for Windows, Mac and Linux?

I have an aplicattion that needs to create a folder to unzip some files, but I want to pass a file path that my application could run, create and save my files in any OS, but I don't know what should be the output Path to do that.
for example:
File folder = new File(outputFolder);
if (!folder.exists()) {
folder.mkdir();
}
What path should I use in outputFolder to create my folder in Documents/UnzipTest in any Operation System (Windows, mac or linux)?
System.getProperty("user.dir");
Or if you're using Java 7
Filesystems.getDefault();
will give you the base directory from which to work.
Use java.io.File#separator :
String userHome = System.getProperty("user.home");
String outputFolder = userHome + File.separator + "dir1";
File folder = new File(outputFolder);
if (!folder.exists()) {
folder.mkdir();
}
I would define a property (in some external properties file, for example) for the root folder, so user can define what is the root folder after the setup or during the installation for all your operations.
Other option is to use user folder (aka home folder) as this root folder (or if above property is not defined), defined in system property user.dir. Or use folder where your application is installed - you can find it e.g. by finding some resource in application jar and then parsing path from returned URL.
With root folder defined, you can use constructor new File(rootFolder, outputFolder); where you can simply use slashes for the path in outputFolder (e.g. /foo/bar). Such path would work for all OSes.
You can use Commons IO from Apache Commons with the classes FileUtils and FilenameUtils.
If you use in your application the UNIX style for your paths, you can transform the path to SO style with the method:
org.apache.commons.io.FilenameUtils.separatorsToSystem(String)
If you want create a directory only if not exists:
org.apache.commons.io.FileUtils.forceMkdir(File)
May you have a path to the directory and may you need a new folder in this directory, you can cancatenate using the method for get the full path:
org.apache.commons.io.FilenameUtils.concat(String, String)
The path returned path is always normalized, contain separators in the format of the system.
May you need the user directory path:
org.apache.commons.io.FileUtils.getUserDirectory()
String outputFolder = System.getProperty("user.home") + File.separator + "Documents" + File.separator +"TestandoUnzip";
The code that I needed. Thanks guys.

How to get a path to a resource/file out of a Java JAR 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"

Categories

Resources