Java application home directory

How do I set a working directory for Java application?

Solution 1: There is a JVM argument which can be used to set working directory for JVM. Solution 2: If it all possible I would rather use a script to run the java application and set the directory in the script: The JNI-solution may affect all kinds of relative paths in your application; for examples the classpath you put in. Java 7 Runtime) — the working directory is my user home directory (/home/angstrem).

How do I set a working directory for Java application?

I’ve written a simple java app, say, with the following code:

String currentDir = new java.io.File(".").getCanonicalPath(); javax.swing.JOptionPane.showMessageDialog(null, currentDir); //This line shows a graphical dialog with the current dir 

When I run it through the terminal, it gives me the directory where the jar-file is located. But when I run it using the gui file manager (that is, right click on the jar-file -> Open With -> OpenJDK Java 7 Runtime) — the working directory is my user home directory (/home/angstrem). How can I set the working directory to be the one, in which the jar-file is situated?

String jarPath = YourClass.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 

replacing YourClass with an actual class defined in your jar.

Читайте также:  Python 3 scrapy примеры

You can then make your file paths be relative to jarPath , and your program will work regardless of its working directory.

You can’t, and you shouldn’t be able to. Consider multi-threading for example. You can only make your application current-path-insensitive.

Changing the current working directory in Java?, But the file handling is done by the operation system. It does not know the new set current directory, unfortunately. The solution may be: File myFile = new File (System.getPropety («user.dir»), «localpath.ext»); It creates a file Object as absolute one with the current directory which is known by the JVM. Code samplepublic static boolean setCurrentDirectory(String directory_name)

Set home or working directory

I have a Java application and have bundled it as App.jar . There are some third party tools I have used (lets call it NumberGenerator ). App.jar starts a process and calls NumberGenerator to get the output. To refer the executable, I have used relative paths new File(«lib/NumberGenerator.exe») and it works all well.

Now on Mac, i have bundled the application using this and it automatically generates an application launcher. When I run by clicking at launcher, it launches the application. But it sets the home directory as ~ i.e. /Users/Jatin and not where the jar file was lying. Hence my application is unable to detect the lib folder (Obviously because it doesn’t lie in that location)

In my Java code, how do I set the home folder as where my jar was lying?

return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()); 

I had a similar problem with linux. I don’t know much about osx not AppBundler but in linux I solved it by creating this script

#!/usr/bin/env sh java -Duser.dir=$(pwd) -jar myapp.jar 

It may work if you manage to run the script by double clicking on it. see How to run a shell script in OS X by double-clicking?

    In Info.plist , specify a java.library.path relative to $JAVAROOT , as described in the articles cite here.

Java . Properties java.library.path $JAVAROOT/  . 
System.setProperty("user.home", "your-dir"); 

Java — Set home or working directory, 0. You can try this: System.setProperty («user.home», «your-dir»); Share. answered Mar 11, 2013 at 7:44. user2030471. I do not know your-dir. It is an application that needs to be given to client. i do now know it pre-hand. He can launch the application from anywhere.

Can you set the current running directory from the java command line?

I am running a Java application from the command line. Can I specify a command line argument to set the current running directory to something other than where the application is actually going to run?

There is a JVM argument -Duser.dir which can be used to set working directory for JVM.

If it all possible I would rather use a script to run the java application and set the directory in the script:

The JNI-solution may affect all kinds of relative paths in your application; for examples the classpath you put in.

If you want to change the current directory, you’ll have to use JNI and invoke a native API from your Java code. For example, for Windows you would use setcurrentdirectory

I found this SO post and it helped me solve my problem. Though I wanted to share something specific about IntelliJ that might trip somebody else up.

I have posted a picture below where the -Duser.dir flag is used and also the Working Directory text field is filled in.

Java application home directory

In this situation, the Working Directory will be set to «JarLearning» rather than «2ndTry».

Io — Change the Current Working Directory in Java, Meanwhile i needed to change the current working directory upon application start. I’ve tried the following options : System.setProperty («user.dir», this.strDestination); But it doesn’t work if we use relative file path, as it refers to the older working directory. Only solution that is working in this regard is using …

Is it possible to set the JVM working directory on startup?

I’m using JNI to startup a JVM and I can’t figure out how to set the working directory. I’ve tried

options[1].optionString = "-Duser.dir=directory"; vm_args.options = options; 

as part of my jni_createjavavm args, but it doesn’t work. The user.dir system property is set to what I specified in my parameters, but the actual relative directory used by things like FileReader is the same directory as whatever I use to call the dll. Is there any way to tell the JVM where the cwd should be on startup?

You could call ‘chdir()’, but it isn’t advisable for programs other than the shell to do so. Just change the current directory yourself in the shell before you start it.

Since the JVM isn’t started as its own executable, your working directory is where your base application is running.

Java — How to change directory and run command on, A Java program is not a shell. While there’s a «current directory» (the value of the user.dir system property), you provide the working directory to each process you launch using Runtime or ProcessBuilder. –

Источник

How to get the current home directory in a Java application

This tutorial explains how to get the current home directory in a Java application. You can determine the current directory your Java application is started in using System.getProperty() method and the user.home property, like this:

How to get the current home directory in a Java application

 String currentDirectory = System.getProperty("user.home");
public class JavaHomeDirectoryExample  public static void main(String[] args)  String currentDirectory = System.getProperty("user.home"); System.out.println("user.dir: " + homeDirectory); > >

This is all about how to get the current home directory in a Java application. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.

Источник

Get the User Home Directory in Java

Get the User Home Directory in Java

  1. Get the User’s Home Directory Using the System.getProperty() Method in Java
  2. Get the User’s Home Directory Using the Apache CommonsIO Library in Java
  3. Get the User’s Home Directory Using the System.getenv() Method in Java
  4. Summary

This tutorial introduces how to get the user home directory in Java and lists some example codes to guide you on the topic.

For a multi-user operating system, there exists a file system directory for every user; this directory is known as the user’s home directory. There are different ways to find the user home directory in Java. Let’s look at each one of them.

Get the User’s Home Directory Using the System.getProperty() Method in Java

The System class in Java has a Properties object used to store different properties and configurations of the current working environment. It also holds the user’s home directory.

We can access these properties by using the getProperty() method of this class. We need to pass the name of the system property that we want to view. In our case, it would be user.home .

The following code demonstrates how it works.

public class Main   public static void main(String[] args)    String userHomeDir = System.getProperty("user.home");  System.out.printf("The User Home Directory is %s", userHomeDir);  > > 
The User Home Directory is C:\Users\Lenovo 

If you’re curious and want to view all the system properties, you can use the getProperties() method. The code for the getProperties() method is shown below.

import java.util.Map; import java.util.Properties; public class Main   public static void main(String[] args)    Properties props = System.getProperties();  for(Map.EntryObject, Object> prop : props.entrySet())  System.out.println("Property: +" + prop.getKey() + "\tValue: " + prop.getValue());  > > 

Get the User’s Home Directory Using the Apache CommonsIO Library in Java

Apache Commons is a very powerful library, and the FileUtils class of the CommonsIO library can be used to fetch the home directory.

We can simply use the getUserDirectory() method of this class to view the user’s home directory. It returns a File object that represents the home directory. We can also get a String path of the home directory by using the getUserDirectoryPath() method.

The code and output for these methods are shown below.

import java.io.File; import org.apache.commons.io.FileUtils; public class Main   public static void main(String[] args)    File homeDir = FileUtils.getUserDirectory();  String homeDirPath = FileUtils.getUserDirectoryPath();  System.out.printf("The User Home Directory is %s\n", homeDir);  System.out.printf("The path of User Home Directory is %s", homeDirPath);  > > 
The User Home Directory is C:\Users\Lenovo The path of User Home Directory is C:\Users\Lenovo 

Get the User’s Home Directory Using the System.getenv() Method in Java

The getenv() method of the System class is used to get the value of system environment variables. We need to pass the name of the environment variable that we want to access.

To get the user’s home directory, we need to use the string USERPROFILE . The following program demonstrates the working of the getenv() method.

public class Main   public static void main(String[] args)    String userHomeDir = System.getenv("USERPROFILE");  System.out.printf("The User Home Directory is %s", userHomeDir);  > > 
The User Home Directory is C:\Users\Lenovo 

You can also use this method to view all the environment variables. Run the following program if you are curious to know more about your system’s environment variables.

import java.util.Map; public class Main   public static void main(String[] args)    MapString, String> envVars = System.getenv();  for(Map.EntryString, String> var : envVars.entrySet())  System.out.println(var.getKey() + " ---> " + var.getValue());  > > 

Summary

In this guide, we learn how to get the user’s home directory in Java. For some Windows platforms running older Java versions (before Java 8), the System.getProperty() method may not give the desired result due to the presence of a bug with ID 4787931.

Another similar bug (Bug ID 6519127) also exists. Because of this, the getProperty() method gives undesirable results. However, both of these bugs have already been resolved.

In most cases, the getProperty() method will work just fine, but we can use the alternative System.getenv() method to get the user home directory.

Related Article — Java Home

Related Article — Java Directory

Источник

Оцените статью