java 运行时.getRuntime().exec()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2146727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Runtime.getRuntime().exec()
提问by JohnRaja
I can not read a file only when database name contains like (new database (myid) etc. I give a following example code:
只有当数据库名称包含(新数据库(myid)等)时,我才能读取文件。我给出了以下示例代码:
dumpCommand = "C:/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump -h"+hostName+user+databaseName;
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(dumpCommand);
InputStream in = proc.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String line =null;
while((line=br.readLine())!=null)
{
//able to read line only when database name like abc,datastore etc...
System.out.println(line);
}
Suppose my database name de mo means when I print line I got database name like de only. Is possible when database name with empty space?
假设我的数据库名称 de mo 意味着当我打印行时,我只得到像 de 这样的数据库名称。当数据库名称为空白时是否可能?
回答by VonC
Are you familiar with the execdouble-quotes bug? (for Runtime.execor ProcessBuilder)
你熟悉exec双引号错误吗?(对于Runtime.exec或ProcessBuilder)
You can try:
你可以试试:
Runtime.getRuntime().exec(new String[] {
"\"C:/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump\"",
"-h",
hostName+user+databaseName});
Just make sure none of your parameters you will have to pass contains double quotes (while notbeginning with double quotes)
(see bug 6511002)
只需确保您必须传递的参数都不包含双引号(但不以双引号开头)
(请参阅 错误 6511002)
Any parameter like:
任何参数,如:
mykey="my value with space"
would be changed internally(by the getRuntime()implementation) into
将在内部(由getRuntime()实现)更改为
"mykey="myvalue with space""
If that is the case, you would need to tokenize the argument:
如果是这种情况,您需要标记参数:
{"mykey=\"my\"" , "\"value with space\""}
回答by helios
Java and the OS need to know what is the command and what are the arguments. So:
Java 和操作系统需要知道什么是命令,什么是参数。所以:
1) You can try to use exec(String, String[])to provide the cmd and the args by separate
1)您可以尝试使用exec(String, String[])分别提供cmd和args
2) You can enclose the mysqldump command and each argument into double quotes for the OS to know their single things.
2) 您可以将 mysqldump 命令和每个参数括在双引号中,以便操作系统了解它们的单个内容。
If you use the (1) option mysqldump program will receive one argument by item in the array, no matter the spaces or whatever.
如果您使用 (1) 选项 mysqldump 程序将在数组中逐项接收一个参数,无论空格或其他什么。
回答by Andreas Dolk
The file path contains blanks, that may be (one of) the problem(s). Try this instead:
文件路径包含空格,这可能是(一个)问题。试试这个:
dumpCommand = "\"C:/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump\" -h"
+hostName+user+databaseName;
and follow phisch's suggstion to print the dumpCommandString to double check that it's a valid mysqldump call
并按照phisch的建议打印dumpCommand字符串以仔细检查它是否是有效的mysqldump调用
回答by burnall
It seems mysqldump syntax is a little bit different. Below is my working example mysqldump -h mysserver -u root -p my_db -R > create_db.sql I think you should use "" or `` for complex names (have not checked myself).
mysqldump 语法似乎有点不同。下面是我的工作示例 mysqldump -h mysserver -u root -p my_db -R > create_db.sql 我认为你应该使用 "" 或 `` 来表示复杂的名称(我自己没有检查过)。
回答by phisch
I am not sure if I understand you question in full, but your dumpCommand seems to concat all Variables (hostName, user, databaseName) to one big string.
我不确定我是否完全理解您的问题,但您的 dumpCommand 似乎将所有变量(主机名、用户、数据库名)连接到一个大字符串。
Do a System.out.println(dumpCommand); and see if that is what you realy want to execute.
做一个 System.out.println(dumpCommand); 看看这是否是你真正想要执行的。
You are probably missing some spaces here.
您可能在这里遗漏了一些空格。
If the "-" itself is a problem, you might want to escape it, otherwise mysqldump will think it is a parameter.
如果“-”本身有问题,你可能要转义它,否则mysqldump会认为它是一个参数。

