简体   繁体   English

调用Runtime.getRuntime()。EXEC()

[英]Runtime.getRuntime().exec()

I can not read a file only when database name contains like (new database (myid) etc. I give a following example code: 我只能在数据库名称包含like(new database(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. 假设我的数据库名称de mo意味着当我打印行时,我得到了像de only这样的数据库名称。 Is possible when database name with empty space? 数据库名称是否有空格?

Are you familiar with the exec double-quotes bug? 你熟悉exec双引号错误吗? (for Runtime.exec or ProcessBuilder ) (对于Runtime.execProcessBuilder

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 not beginning 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\""}

Java and the OS need to know what is the command and what are the arguments. Java和操作系统需要知道命令是什么以及参数是什么。 So: 所以:

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程序将在数组中按项接收一个参数,无论空格还是其他。

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 dumpCommand String to double check that it's a valid mysqldump call 并按照phisch的suggsting打印dumpCommand String以仔细检查它是否是一个有效的mysqldump调用

It seems mysqldump syntax is a little bit different. 看来mysqldump语法有点不同。 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 -h mysserver -u root -p my_db -R> create_db.sql我认为你应该使用“”或``来表示复杂的名字(还没有检查过我自己)。

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似乎将所有变量(hostName,user,databaseName)连接成一个大字符串。

Do a System.out.println(dumpCommand); 做一个System.out.println(dumpCommand); and see if that is what you realy want to execute. 看看你是否真的想要执行它。

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会认为它是一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM