Runtime.getruntime (). EXEC command contains parentheses problem __runtime

Source: Internet
Author: User

When you write a batch execution Bat tool, you think of the method of execution in the timer gadget that you wrote earlier.

Use the Runtime.getruntime (). Exec method.

This allows you to execute the command line just like a DOS window.


GetRuntime returns the instantiated object of the runtime, and the Exec method returns the Process object.


View the process class to discover:

The Processbuilder.start () and Runtime.exec methods create a native process and return a instance of a subclass of Proces s that can is used to control the process and obtain information about it. The class process provides methods for performing input from the process, performing output to the process, and waiting for th E process to complete, checking the exit status of the process, and destroying (killing) the process.

The methods that create processes could not work. Special processes on certain native platforms, such as native wind Owing processes, daemon processes, Win16/dos processes on Microsoft Windows, or shell scripts.


Method creates an object that local processes return the process subclass to control processes and obtain information. The Process class provides methods for input from processes, process output, processes waiting to end and process exit states, and destruction (kill) processes. The method of creating a process may not work well for special processes on the local platform. such as the local window process, daemon, Microsoft Windows Win16/dos process or shell script.


First write a simple test.bat in C disk.

echo AAAA
Pause

It is then invoked in Java:

Import java.io.IOException;


public class Testruntime {public
	static void Main (string[] args) {
		try {
			runtime.getruntime (). EXEC ("cmd/c s Tart C:\\test.bat ");
		} catch (IOException e) {
			//TODO auto-generated catch block\
			System.out.println ("File not Found");
		}
}

This is the normal execution.



The problem, however, is that when the bat file has parentheses, the execution is different, and the original Test.bat is changed to test (s). bat after execution.



I saw this blog in the back, http://blog.sina.com.cn/s/blog_656977f40101d05p.html.

But there are also errors in the blog, the file path should be used \ or/.

Runtime.getruntime (). EXEC ("cmd/c start call \" C:\\test (s). bat\ "");

After this normal access, call normally calls the Parenthesized bat file.


In fact, this problem has entangled me for a long time, indeed some orders do not work very well, has been to solve this problem. But I have found the solution to this problem, otherwise only in this problem more and more deep, in fact, when a problem quickly to the corner of the time, to change the way of thinking to solve the problem,


The original idea:

The problem now is the inability to recognize the bat file with parentheses in the filename--how to make the Exec method recognized.


New ideas:

The problem now is that you can't identify the bat file with parentheses in the filename--can you get rid of the parentheses in the bat file name?


In fact, this idea is by this man's blog to Think of:

http://blog.csdn.net/xulianboblog/article/details/18360131

Different way to solve the problem.


Also: Http://blog.163.com/jackswu@yeah/blog/static/1406291232012575743301/wrote some of the commands, more useful.


Java runtime.getruntime (). EXEC (CommandText) can invoke the execute CMD instruction.

cmd/c dir closes the command window after the dir command has been executed.
cmd/k dir does not close the command window until the dir command is executed.

cmd/c start dir opens a new window and executes the dir instruction, and the original window closes.
cmd/k start dir opens a new window and executes the dir instruction, and the original window does not close.
Note : When you add start, you will open a new window, and you can use CMD/? To view help information.



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.