User dir user home java

Java «user.dir» property — what exactly does it mean?

But, when I am in dir1 and make a jar file from dir2, it makes the jar file in dir1 and the jar file has error : I thought it works if I change current directory by java code. Solution 2: As previous answer says user.dir is «current user directory» Just look at next examples: cd /path/to/your/jar $JAVA_HOME/bin/java -jar

Java «user.dir» property — what exactly does it mean?

I want to use user.dir dir as a base dir for my unit tests (that creates a lot of files). Is it correct that this property points to the current working directory (e.g. set by the ‘cd’ command)?

It’s the directory where java was run from, where you started the JVM. Does not have to be within the user’s home directory. It can be anywhere where the user has permission to run java.

So if you cd into /somedir , then run your program, user.dir will be /somedir .

A different property, user.home , refers to the user directory. As in /Users/myuser or /home/myuser or C:\Users\myuser .

See here for a list of system properties and their descriptions.

user.dir is the «User working directory» according to the Java Tutorial, System Properties

Читайте также:  Php stack trace notice

Typically this is the directory where your app (java) was started (working dir). «Typically» because it can be changed, eg when you run an app with Runtime.exec(String[] cmdarray, String[] envp, File dir)

System.getProperty(«user.dir») fetches the directory or path of the workspace for the current project

How to change user.dir ( or current directory in, I thought it works if I change current directory by java code. If it works from command prompt, it will be correct in java code. C:\Users\username\Desktop>jar cfe Main.jar Main F:\Java\Files\Main.class C:\Users\username\Desktop>java -jar F:\Java\Files\Main.jar Error: Could not …

Trying to understand on what «user.dir» property actually means?

So basically wen I googled on what the «user.dir» property does I got this link to a similar question on stackexchange: Java “user.dir” property — what exactly it means?

So basically the answer given was : It’s the directory where Java was run from, where you started the JVM. Does not have to be within the user’s home directory. It can be anywhere where the user has permission to run Java.

Which is okay, but then I found this other link in the android context: Why does System.getProperty(“user.dir”) return “/” As the documentation says, the user.dir property is the user working directory, which is not necessarily the same as the directory where your apk is placed.

So the first best voted answer in the first link says the directory does not have to be within the users home directory while the other says «it is the user working directory.»

Now I am terribly confused and need help on this.

A home directory is associated with a user (for a user MIke, this is often something like /home/mike ).

A working directory is associated with a running process ; in a shell this is often printed as part of the prompt, for other processes there is usually no direct way of observing it. A user typically starts a shell session in their home directory but then can move away from it.

The directory where Java is installed is a third, unrelated thing (and the directory in which a java application or library is installed could be yet another, unrelated fourth directory). But as the documentation says,

By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

In other words, user.dir is the working directory of the process that started the Java process at the time when it started the process . If this was a shell, it is the directory to which the shell user had last switched. If Java was called from another program, e.g. a window manager, it is whatever that program had as its working directory.

As previous answer says user.dir is «current user directory» Just look at next examples:
cd /path/to/your/jar
$JAVA_HOME/bin/java -jar myprogram.jar
(user.dir = /path/to/your/jar)

cd $JAVA_HOME/bin
java -jar /path/to/your/jar/myprogram.jar
(user.dir = $JAVA_HOME/bin)

cd ~
$JAVA_HOME/bin/java -jar /path/to/your/jar/myprogram.jar
(user.dir = user home directory)

Java — What is the difference between «user.dir» and, I think that the purpose of «user.dir» property is the same as pwd command in Linux or GetCurrentDirectory in Windows. I think it is more clear this way because we clearly use pwd when we want to get the (absolute) path of the current directory. In contrast, . is a shorthand for the current dir when you are not …

Java user.dir is not always correct

Im making a basic .bat file that runs the application. This is to get auto startup feature.

However for some reason System.getProperty(«user.dir») does not always get the correct path to the program.

Basically i am saving this to the .bat file:

protected final String fileSeparator=System.getProperty("file.separator"); out.println("@echo off"); out.println("start " + System.getProperty("user.dir") + fileSeparator +"App.jar"); out.println("exit"); 

On Windows server it returns the correct path but on Vista it does not.

Any ideas how i could get this to work on all versions of Windows?

You can do this by providing user.dir when you start your Java program like this

java somepackage.Main -Duser.dir=C:/Users/myUser 

user.dir = User working directory [from documentation ]

It is difficult to take decision based on value of this variable. Depending on the program starting the «java», it could have different values. For example, a bat file could have different working directory from invoked from different command-windows.

You could use %~dp0 to get location of the batch script and then put other paths relative to this.

Another option is to use tools like launch4j, which allows an easy way to control program directory : how to get the path to the executable when using launch4j?

Java — Why user.dir could be set to «.»?, Add a comment. -1. Home directory is set in /etc/passwd file. parv1616@parv:~$ cat /etc/passwd | grep parv1616. parv1616:x:1000:1000:Parvinder Singh. /home/parv1616 :/bin/bash. You just need to change it with the directory you wish. It can also be changed with usermod …

How to change user.dir ( or current directory in command prompt ) in java?

When I go to a directory and make a jar file in command prompt, it works correctly. But, when I am in dir1 and make a jar file from dir2, it makes the jar file in dir1 and the jar file has error :

I thought it works if I change current directory by java code. If it works from command prompt, it will be correct in java code.

C:\Users\username\Desktop>jar cfe Main.jar Main F:\Java\Files\Main.class C:\Users\username\Desktop>java -jar F:\Java\Files\Main.jar Error: Could not find or load main class Main 

You are specifying that the Main.jar be generated in the current directory. Include the full path of the jar file when you are generating it:

C:\Users\username\Desktop>jar cfe F:\Java\Files\Main.jar Main F:\Java\Files\Main.class 

Java — System.getProperty(«user.dir») gives different path, Which is root directory of project in which java class files lies having System.getProperty(«user.dir») line of code. On the other hand if I run System.getProperty(«user.dir») code from some servlet/ service/ business/ dac java class then it prints as below: D:\eclipse-jee-mars-2-win32\eclipse. Which is root …

Источник

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

Источник

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