How does the Java virtual machine determine the value of the user.dir System property?

I am running a simple Java program with the following directory structure:

MyProject (A project in my Eclipse IDE) '-- src '-- Hello.java 

In Hello.java, I am printing the value of the 'user.dir' System property.

 System.out.println(System.getProperty("user.dir")); 

The compiled file for my class is stored in the MyProject\bin .

When I run this class from Eclipse (right-click on the source file and select "Run as Java Application"), it prints the path to the folder "MyProject", that is, D:\Projects\Workspace\MyProject in the console window.

Then I used a command window to run the same program. This is what I typed in the window:

 D:\Projects\Workspace\MyProject\bin>java Hello 

and console output: D:\Projects\Workspace\MyProject\bin

bin was added to the previous value for user.dir.

Next, to check more, this time I ran a Java command from another folder in the command window:

 D:\Projects\Workspace\MyProject>java -classpath D:\Projects\Workspace\MyProject\bin Hello 

This time in the command window: D:\Projects\Workspace\MyProject

This value changes when I change the folder in the command window, and when I run the program from Eclipse, the value for user.dir is the project folder. So I would like to understand what is the basis for getting the value of "user.dir"? How does the JVM decide what the value for user.dir should be?

+4
source share
5 answers

As the java.lang.System specification defines , the user.dir property returns the current working directory (that is, the current directory when the JVM starts):

user.dir user running directory

I do not see anything contradictory in your example. The only thing that is unclear here is the name of the property. I don’t understand why they decided to put a β€œuser” here.

Similarly, if you run the same Java program from a completely different path, you will get a different path as the result. Try it yourself:

  c: cd c:\ java -cp D:\Projects\Workspace\MyProject\bin Hello 

What Eclipse does before running your program is like:

  d: cd d:\projects\workspace\myproject java -cp d:\projects\workspace\myproject\bin Hello 
+9
source

The user.dir property is defined as the current working directory. Javadoc for System details various features of the system.

Perhaps you really need user.home?

+1
source

In addition to other answers (which, in my opinion, fully answer the question):

If you are really looking for a way to access your class files, do not use this property (or any property at all). Use

 System.out.println( Hello.class.getResource("/") ); 

to show the path to the root of your package hierarchy, and the path without / at the beginning will refer to the directory of your Hello.class . (It works when they are inside the jar file.)

+1
source

This is a custom working directory. So, when you start it from the command line, you change your working directory, whereas in the case of eclipse, your working directory is the home directory of the project. Link: - http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html

0
source

Default System Properties

I always try to find the default properties in Java and have to write a program for this. If there is something online, I can avoid this fuss. Its good that he is here :)

Code:

 public class Test { public static void main(String[] args) { Properties prop = System.getProperties(); prop.list(System.out); } } 

Exit for Windows XP

 -- listing properties -- java.runtime.name=Java(TM) SE Runtime Environment sun.boot.library.path=C:\Program Files\Java\jre6\bin java.vm.version=19.0-b09 java.vm.vendor=Sun Microsystems Inc. java.vendor.url=http://java.sun.com/ path.separator=; java.vm.name=Java HotSpot(TM) Client VM file.encoding.pkg=sun.io user.country=US sun.java.launcher=SUN_STANDARD sun.os.patch.level=Service Pack 3 java.vm.specification.name=Java Virtual Machine Specification user.dir=C:\workspace\Test java.runtime.version=1.6.0_23-b05 java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment java.endorsed.dirs=C:\Program Files\Java\jre6\lib\endorsed os.arch=x86 java.io.tmpdir=C:\DOCUME~1\Name~1\LOCALS~1\Temp\ line.separator= java.vm.specification.vendor=Sun Microsystems Inc. user.variant= os.name=Windows XP sun.jnu.encoding=Cp1252 java.library.path=C:\Program Files\Java\jre6\bin;.;C:\W... java.specification.name=Java Platform API Specification java.class.version=50.0 sun.management.compiler=HotSpot Client Compiler os.version=5.1 user.home=C:\Documents and Settings\User Name user.timezone= java.awt.printerjob=sun.awt.windows.WPrinterJob file.encoding=Cp1252 java.specification.version=1.6 user.name=User Name java.class.path=C:\workspace\Test\bin java.vm.specification.version=1.0 sun.arch.data.model=32 java.home=C:\Program Files\Java\jre6 java.specification.vendor=Sun Microsystems Inc. user.language=en awt.toolkit=sun.awt.windows.WToolkit java.vm.info=mixed mode, sharing java.version=1.6.0_23 java.ext.dirs=C:\Program Files\Java\jre6\lib\ext;C:... sun.boot.class.path=C:\Program Files\Java\jre6\lib\resour... java.vendor=Sun Microsystems Inc. file.separator=\ java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport... sun.cpu.endian=little sun.io.unicode.encoding=UnicodeLittle sun.desktop=windows sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+m... 
0
source

Source: https://habr.com/ru/post/1339812/


All Articles