3

I'm trying to run a bash script file in Java program and I'm using this way:

Runtime rt = Runtime.getRuntime();

try {           
  Process proc = rt.exec("THE PATH FILE");        
  BufferedReaderinput=newBufferedReader(newInputStreamReader(proc.getInputStream()));
  String line=null;
  while((line=input.readLine()) != null) {
    System.out.println(line);
   }
} catch (IOException ex) {
  System.out.println(ex.toString());
}

but I'm using an input in my bash script as $1 when I'm running it from command line and I want to use it in my Java program to take the correct output . How can I do this in my Java program?

3
  • 3
    I don't think this question is helped by the javascript tag; edited to java. Commented Nov 10, 2012 at 15:17
  • using $1 in your bash script means that you read the first argument. as i understand it, you try to input your data through stdin instead. Commented Nov 10, 2012 at 15:19
  • dup? Commented Nov 10, 2012 at 15:25

2 Answers 2

1

Read the input in your Java app, then launch a new process that looks something like this:

bash myfile.sh arg[0] arg[1] arg[n]
Sign up to request clarification or add additional context in comments.

Comments

1

As suggested by Mike Thomsen, you can get the command line arguments into your java application and pass them on to the script file something like this.

StringBuilder cmdLineArgs = new StringBuilder();  

for(String arg:args){   //in case you may have variable number of arguments
    cmdLineArgs.append(arg).append(" ");
}

Now pass the arguments collected in cmdLineArgs to your script.

Process proc = rt.exec("YOUR SCRIPT " + cmdLineArgs.toString()); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.