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

  • SEARCH
  • Home
  • 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 7744845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:51:58+00:00 2026-06-01T09:51:58+00:00

I am wondering is there any way to execute following shell script, which waits

  • 0

I am wondering is there any way to execute following shell script, which waits for user input using java’s Runtime class?

#!/bin/bash

echo "Please enter your name:"
read name
echo "Welcome $name"

I am using following java code to do this task but it just shows blank console.

public class TestShellScript {
public static void main(String[] args) {

        File wd = new File("/mnt/client/");
           System.out.println("Working Directory: " +wd);
           Process proc = null;

           try {
               proc = Runtime.getRuntime().exec("sudo ./test.sh", null, wd);

           } catch (Exception e) {
             e.printStackTrace();
             }


}

}

Thing is when I execute above program, I believed it will execute a shell script and that shell script will wait for user input, but it just prints current directory and then exits. Is there any way to do this or it is not possible at all in java?

Thanks in advance

  • 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-06-01T09:51:59+00:00Added an answer on June 1, 2026 at 9:51 am

    The reason it prints the current dir and exits is because your java app exits. You need to add a (threaded) listener to the input and error streams of your created process, and you’ll probably want to add a printStream to the process’s output stream

    example:

    
    
                proc = Runtime.getRuntime().exec(cmds);
                PrintStream pw = new PrintStream(proc.getOutputStream());
                FetcherListener fl = new FetcherListener() {
    
                    @Override
                    public void fetchedMore(byte[] buf, int start, int end) {
                        textOut.println(new String(buf, start, end - start));
                    }
    
                    @Override
                    public void fetchedAll(byte[] buf) {
                    }           
                };
                IOUtils.loadDataASync(proc.getInputStream(), fl);
                IOUtils.loadDataASync(proc.getErrorStream(), fl);
                String home = System.getProperty("user.home");
                //System.out.println("home: " + home);
                String profile = IOUtils.loadTextFile(new File(home + "/.profile"));
                pw.println(profile);
                pw.flush();
    

    To run this, you will need to download my sourceforge project: http://tus.sourceforge.net/ but hopefully the code snippet is instructive enough that you can just adapt to J2SE and whatever else you are using.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to execute the following command through C#? .\binary.exe < input
I was wondering if there is a way to execute script within a ajax
I was wondering what would be the best way to execute a java-script code
I am wondering is there any way we can test the font size/color of
Wondering if there is any way to get the lambda expressions that result from
Just wondering if there is any way to get the NS records in C#.
I was wondering if there was any way of grabbing a variable from the
I'm wondering if there's any way for a Rails asset to vary according to
I was wondering if there's any way to have a scipy.sparse.csc_matrix format for mlpy
I was wondering if there is any way to SELECT the first number that

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.