Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Invoking Script in Java.

Posted on 1998-06-26
1
Medium Priority
?
281 Views
Last Modified: 2010-03-30
Can anyone just let me know how to invoke a shell script in a Java Code. Please provide me the syntax & sample for the same.
0
Comment
Question by:uramsi01
1 Comment
 
LVL 5

Accepted Solution

by:
fontaine earned 150 total points
ID: 1223801
Here is an example.

Shell script (test.sh):

#!/bin/sh
env > test.txt

Java prog:

import java.io.*;

public class ShellTest {

    public static void main(String args[]) throws IOException, InterruptedException {
         Runtime runtime = Runtime.getRuntime();
         String command = "test.sh"
         Process process = runtime.exec(command);
         process.waitFor(); // block until completion
         return;    
   }
}

Compile and run, "test.txt" appears with the environment in it.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question