简体   繁体   中英

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. I am doing this to make a program I can use to ssh into another pc (just as a project). 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");
}

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. 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. which will be taken the effect immediately.

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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