Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中执行命令提示符_Java - Fatal编程技术网

在java中执行命令提示符

在java中执行命令提示符,java,Java,我在java中执行这些命令,并得到以下错误 symbol : method write(java.lang.String) location: class java.io.OutputStream out.write("tabcmd publish C:\\Users\\c200433\\Desktop\\Ana\\".getBytes()+filename+" --db-username IIP_RBM_USER --db-password Ytpqxsb9dw".getBytes());

我在java中执行这些命令,并得到以下错误

symbol  : method write(java.lang.String)
location: class java.io.OutputStream
out.write("tabcmd publish C:\\Users\\c200433\\Desktop\\Ana\\".getBytes()+filename+" --db-username IIP_RBM_USER --db-password Ytpqxsb9dw".getBytes());



String command = "cmd /c start cmd.exe";
    Process child = Runtime.getRuntime().exec(command);
    OutputStream out = child.getOutputStream();

out.write("tabcmd publish C:\\Users\\c200433\\Desktop\\Ana\\".getBytes()+filename+" --db-username IIP_R --db-password Ytb9dw".getBytes());

How Do i resolve this issue.

OutputStream
除了
字节[]
,而不是
字符串

out.write("tabcmd publish C:\\Users\\c200433\\Desktop\\Ana\\".getBytes() +
           filename + " --db-username IIP_R --db-password Ytb9dw".getBytes());
我不认为
javac
会允许
“..”.getBytes()+String
,因为它是
byte[]+String
。+运算符是为
数字
字符串
布尔值
定义的。不是
字节[]

相反,您必须:

  • 或者在您的流上使用一个
  • 要么先连接到字符串,然后对结果调用getBytes
使用PrintStream:

try (PrintStream ps = new PrintStream(child.getOutputStream())) {
  ps.append("tabcmd publish C:\\Users\\c200433\\Desktop\\Ana\\")
    .append(filename)
    .append(" --db-username IIP_R --db-password Ytb9dw");
}
使用
字符串
串联:

out.write( ("tabcmd publish C:\\Users\\c200433\\Desktop\\Ana\\" +
            filename + " --db-username IIP_R --db-password Ytb9dw").getBytes(Charset.forName("utf-8")));
我使用和不使用,但这取决于您的流程(例如:它是否接受UTF-8?)。但是,您必须记住Java字符串是Unicode序列,因此getBytes()可能使用Windows cp1252(Windows上的默认值)


我没有签入
PrintStream
源代码,但是如果它使用
getBytes()
而没有
Charset
,那么使用时应该使用带有适当字符集的。

不要对命令使用单个
字符串,空格会把事情搞砸。相反,将每个参数作为一个单独的
字符串传递
,并使用
ProcessBuilder
可以显示堆栈跟踪吗?