简体   繁体   English

如何将命令传递到已经在Java(Linux)中运行的终端中

[英]How could I pass in commands into a terminal that is already running in java (linux)

I have been researching how to run a terminal command in java. 我一直在研究如何在Java中运行终端命令。 I am doing this to make a program I can use to ssh into another pc (just as a project). 我这样做是为了制作一个程序,我可以使用它将ssh转换为另一台pc(就像一个项目一样)。 How could I keep continuing putting commands in this terminal? 我如何继续在该终端中继续输入命令? If I run this I get a message to put in my password and if I do so it will print out what the messages the terminal spits out at the : while((line = in.readLine()) != null){ System.out.println(line + "\\n");
}
如果运行此命令,我会收到一条输入密码的消息,如果这样做,它将打印出终端在以下位置吐出的消息: while((line = in.readLine()) != null){ System.out.println(line + "\\n");
}
while((line = in.readLine()) != null){ System.out.println(line + "\\n");
}

line, but a few seconds after that my program will stop working. 行,但是在那之后的几秒钟我的程序将停止工作。 I currently have a GUI that is just a button and if I press the button it will run this code. 我目前有一个GUI,它只是一个按钮,如果按下按钮,它将运行此代码。 Could someone help me to fix the issue of it stopping and give me information on how I could continue to put commands into the terminal? 有人可以帮助我解决停止问题,并提供有关如何继续在终端中输入命令的信息吗? Thanks. 谢谢。

    Process p = null;
    String[] command = {"/bin/sh",  "-c", "ssh 192.168.2.100"};
    ProcessBuilder pb = new ProcessBuilder(command);

    String line = null;

    try {
        term = pb.start();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    InputStream is = p.getInputStream();

    BufferedReader in = new BufferedReader(new InputStreamReader(is));

    try {
        while((line = in.readLine()) != null){
            System.out.println(line + "\n");    
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

You can not pass the argument to live JVM, But there is way in which you can modify the some of the parameters which done using JMX , with this you can connect to live JVM and send the parameters. 您不能将参数传递给实时JVM,但是可以通过某种方式修改使用JMX完成的一些参数,从而可以连接到实时JVM并发送参数。 which will be taken the effect immediately. 这将立即生效。

Hope that helps 希望能有所帮助

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

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