同样的命令在命令行里好使 但是在Runtime.getRuntime().exec(cmd);中执行无效果?

mopishv0 2011-08-30 01:14:23
下面那段代码用于通过Runtime.getRuntime().exec向mysql导入 .sql文件 经试验mysql -uroot -p123456 </lqa.sql命令在inux控制台下可以导入
但是执行代码后无效果
如果将"mysql -uroot -p123456 </lqa.sql"改为"mysql -uroot -p123456 < /lqa.sql" 或其他状态
则会出现 代码下面的 mysql帮助信息
这是为什么呢?同样的命令在命令行里好使 但是在Runtime.getRuntime().exec(cmd);中执行无效果?


private static void importSql(String installFilesPath, String[] sqlPaths) throws Exception {
for(String sqlPath : sqlPaths) {
// String cmd = "mysql -u "+USER+" -p"+PASSWORD+"\nsource "+installFilesPath+sqlPath+";\nexit;\n";
String cmd = "mysql -uroot -p123456 </lqa.sql";
System.out.println(cmd);
// System.out.println(new String(cmd.getBytes("GB2312"),"UTF-8"));
// System.out.println(new String(cmd.getBytes("UTF-8"), "UTF-8"));
Process process = Runtime.getRuntime().exec(cmd);
// Process process = Runtime.getRuntime().exec("mysql",
// new String[]{"-uroot", "-p123456", "</lqa.sql"});
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String output = null;
while ((output = bufferedReader.readLine()) != null){
System.out.println(output);
}
process.waitFor();
break;
}
}





Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
-A, --no-auto-rehash
No automatic rehashing. One has to use 'rehash' to get

......以后忽略
...全文
694 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mopishv0 2011-09-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 softroad 的回复:]

你命令的意思就是将执行的信息输出到sql文件, < /lqa.sql不会是多了个空格吧。
[/Quote]
那条命令在命令行里是好使的
最后放到shell文件里也是那样的格式
softroad 2011-08-30
  • 打赏
  • 举报
回复
你命令的意思就是将执行的信息输出到sql文件, < /lqa.sql不会是多了个空格吧。
softroad 2011-08-30
  • 打赏
  • 举报
回复
命令的有些特殊字符需要转义。
mopishv0 2011-08-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yangting_lisa 的回复:]

lz先检查下 show variables like '%autocommit%';是true还是false,这个一定要是true。
还是"mysql -uroot -p123456 </lqa.sql";中的<应该是转义字符吧,java中不是这个。至于ls的说的很有道理,但是你先要检查自己的问题。lz加油
[/Quote]
先生成SHELL了
之前我用SQL LOADER 和很多其他命令也都是好使的 很奇怪 < 不是转义字符吧
小笨熊 2011-08-30
  • 打赏
  • 举报
回复
lz先检查下 show variables like '%autocommit%';是true还是false,这个一定要是true。
还是"mysql -uroot -p123456 </lqa.sql";中的<应该是转义字符吧,java中不是这个。至于ls的说的很有道理,但是你先要检查自己的问题。lz加油
小笨熊 2011-08-30
  • 打赏
  • 举报
回复
关注下,我之前用过其他的指令都是可以的,为啥你的sql不行,难道你的数据已经写进去了,但是没有提交
ydj552110737 2011-08-30
  • 打赏
  • 举报
回复
新手路过
woodenhouse 2011-08-30
  • 打赏
  • 举报
回复
在java的runtime里执行命令行任务,如果命令有比较多得空格或者牵涉到输入输出重定向/管道命令时,直接写入命令会出错,因为空格在java的runtime里会有其它的意义,你可以尝试2种方法
1是使用runtime(String cmd[])这个重载函数,将一条命令写入一个String数组里
2就是上面几位的说法,写成批处理或者shell脚本,可以减少命令复杂度,单纯的 命令名 变量值 这样的写法是不会报错的
安心逍遥 2011-08-30
  • 打赏
  • 举报
回复
String cmd = "BatchProcessing.bat";
Process process = Runtime.getRuntime().exec(cmd);
直接执行批处理吧

错误信息业说的很明白

This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Usage: mysql [OPTIONS] [database]
打油的程序员 2011-08-30
  • 打赏
  • 举报
回复
//一种又简单又有效的方法:写批处理文件
String cmd = "BatchProcessing.bat";
Process process = Runtime.getRuntime().exec(cmd);

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧