Process working directory in JVM (Java Virtual machine) tutorial

Source: Internet
Author: User
Tags root directory

Every time we run our Java program with a Java command, will open a process in the JVM, for each process, there will be a corresponding working directory, the working directory is set up when the virtual machine initialization, by default, the working directory is our engineering root directory, such as:

/home/test/project-

--bin

--divinemind.onlyfun.test.helloworld.java

--src

If we start HelloWorld with the following command, the root directory of the process is the resource required for the operation in/home/test/project/bin,helloworld, such as files, which will be searched in this directory.

Under Windows, the situation is the same.

We can pass

---------------------

System.getproperty ("User.dir");

----------------------

Get the working directory of the current process, and when we want to change the working directory, we can also

---------------------

System.setproperty ("User.dir", "/home/test/xxxxxxx");

---------------------

Sets the working directory for the current process, but, in fact, User.dir This system environment can not be reset, a lot of problems, the most common problem, all threads under the current process are using this environment variable, if modified, the danger is conceivable, the sun's JDK this is estimated to be a small bug. So, System.setproperty This method just lets us Look, it doesn't really work. In Sun's JDK1.4, the working directory of the current process is not modified, and in JDK1.5, Processbuilder has improved in this regard.

When we don't have the option to use the resources in the non working directory, for example: Java call A, a using b,b and a in the same directory, A and B are not in the current process of the working directory, so that the program must run a problem, this time there are two ways to solve

1. All paths are written in absolute paths

All path write absolute path can be implemented, but the program in the deployment of a lot of trouble, maintenance is difficult, but the method is simple, intuitive.

2. A new subprocess is opened in the current process, modifying the working directory of the child process

The new method of opening a subprocess in Java is related to the JDK version:

A In JDK1.4:

-------------------------

Process process = System.getRuntime.exec (ARG1,ARG2,ARG3);

Arg1: System command

ARG2: Environment variable for command run

Arg3: The working directory of the subprocess, where you can set up the working directory that we want, to achieve the method of working with a non-parent process.

-------------------------

B In JDK1.5:

In this release, we can use Processbuilder to open a new JVM process.

The following code can implement our functionality here:

-------------------------

Processbuilder PB = new Processbuilder ("MyCommand", "MyArg1", "myArg2");

Mydir is the working directory of the current process, if not set, that is the default work//directory for the parent process

Pb.directory ("Mydir");

Process p = Pb.start ();

-------------------------

Java in peacetime with the operating system is really very little relationship, we rarely encounter problems, but if we call the operating system command what the time, the problem is more, but if we know more about the JVM, the issue is easier to solve.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.