Java set working 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.

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

Читайте также:  Javascript цикл с шагом

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 set working 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. –

Источник

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