Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6654069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:20:44+00:00 2026-05-26T01:20:44+00:00

How do I run multiple commands in SSH using Java runtime? the command: ssh

  • 0

How do I run multiple commands in SSH using Java runtime?

the command: ssh user@127.0.0.1 ‘export MYVAR=this/dir/is/cool; /run/my/script
/myscript; echo $MYVAR’

@Test
  public void testSSHcmd() throws Exception
  {
    StringBuilder cmd = new StringBuilder();

    cmd.append("ssh ");
    cmd.append("user@127.0.0.1 ");
    cmd.append("'export ");
    cmd.append("MYVAR=this/dir/is/cool; ");
    cmd.append("/run/my/script/myScript; ");
    cmd.append("echo $MYVAR'");

    Process p = Runtime.getRuntime().exec(cmd.toString());
  }

The command by its self will work but when trying to execute from java run-time it does not. Any suggestions or advice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:20:45+00:00Added an answer on May 26, 2026 at 1:20 am

    Use the newer ProcessBuilder class instead of Runtime.exec. You can construct one by specifying the program and its list of arguments as shown in my code below. You don’t need to use single-quotes around the command. You should also read the stdout and stderr streams and waitFor for the process to finish.

    ProcessBuilder pb = new ProcessBuilder("ssh", 
                                           "user@127.0.0.1", 
                                           "export MYVAR=this/dir/is/cool; /run/my/script/myScript; echo $MYVAR");
    pb.redirectErrorStream(); //redirect stderr to stdout
    Process process = pb.start();
    InputStream inputStream = process.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String line = null;
    while((line = reader.readLine())!= null) {
        System.out.println(line);
    }
    process.waitFor();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is possible to run multiple commands or sql script using OLE DB? For
I need to run multiple commands in sequence using NSTask and was wondering what
When validating user inputs in an application that is to be run across multiple
I've run across this sort of thing on multiple websites and was wondering what
Following on from this question: Run WCF ServiceHost with multiple contracts Are there any
I've been trying to find something that will let me run multiple commands on
I already have an ssh agent set up, and I can run commands on
I want to run the same set of Unix commands on multiple machines. I
How can I use SSH to send multiple commands to a networked system, logging
I'm still new to Unix. Is it possible to run multiple commands of Unix

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.