我正在使用2按钮Run and Commit处理应用程序。 当我按Run时,我的应用程序在sql plus中运行一个脚本。我的想法是,当我按下提交按钮时,我想保留在sql plus中并进行提交。我也希望看到每个命令的输出。事情正常,直到我按下提交。输出没有显示..
我的运行按钮操作:
package pachet1;
import java.util.Scanner;
import javafx.scene.control.TextArea;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import java.io.*;
import java.net.MalformedURLException;
import java.nio.file.Files;
public class MyExec {
public static ProcessBuilder builder;
public static Process p;
public static BufferedReader bri;
public static BufferedReader bre;
public static BufferedWriter p_stdin;
public static void executeScript(String scriptName, String alias, String path, TextArea txtArea) throws IOException
{
NtlmPasswordAuthentication userCred = new NtlmPasswordAuthentication("domain",
"username", "password");
SmbFile smbFile=new SmbFile("path" + scriptName, userCred);
File file = new File("D://" + scriptName);
try (InputStream in = smbFile.getInputStream()) {
Files.copy(smbFile.getInputStream(), file.toPath());
}
//init shell
builder = new ProcessBuilder( "cmd" );
builder.redirectErrorStream(true);
try {
p = builder.start();
}
catch (IOException e) {
//System.out.println(e);
}
//get stdin of shell
p_stdin =
new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
// execute the desired command (here: ls) n times
int n=1;
try {
//single execution
p_stdin.write("sqlplus sys/sys@" + alias +" as sysdba");
p_stdin.newLine();
p_stdin.flush();
p_stdin.write("@"+file.toPath());
p_stdin.newLine();
p_stdin.flush();
// p_stdin.write("commit;");
// p_stdin.newLine();
// p_stdin.flush();
}
catch (IOException e) {
//System.out.println(e);
}
// finally close the shell by execution exit command
try {
p_stdin.write("");
p_stdin.newLine();
p_stdin.flush();
}
catch (IOException e) {
System.out.println(e);
}
// write stdout of shell (=output of all commands)
String line;
bri = new BufferedReader
(new InputStreamReader(p.getInputStream()));
bre = new BufferedReader
(new InputStreamReader(p.getErrorStream()));
while ((line = bri.readLine()) != null) {
txtArea.appendText(line + "\n");
System.out.println(line + "\n");
}
//bri.close();
while ((line = bre.readLine()) != null) {
txtArea.appendText(line + "\n");
System.out.println(line + "\n");
}
//bre.close();
System.out.println("Done.");
}
}
我的提交按钮操作:
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
MyExec.p_stdin.write("commit");
MyExec.p_stdin.newLine();
MyExec.p_stdin.flush();
MyExec.p_stdin.write("exit");
MyExec.p_stdin.newLine();
MyExec.p_stdin.flush();
//txtArea.appendText("Commit complete.");
String line;
MyExec.bri = new BufferedReader
(new InputStreamReader(MyExec.p.getInputStream()));
MyExec.bre = new BufferedReader
(new InputStreamReader(MyExec.p.getErrorStream()));
while ((line = MyExec.bri.readLine()) != null) {
txtArea.appendText(line + "\n");
System.out.println(line + "\n");
}
MyExec.bri.close();
while ((line = MyExec.bre.readLine()) != null) {
txtArea.appendText(line + "\n");
System.out.println(line + "\n");
}
MyExec.bri.close();
System.out.println("Done.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
public void commit(ActionEvent e)
{
t1.start();
}
所以我需要在该线程中从运行按钮获取进程输出并继续显示它,但它不起作用..