Run Unix commands from Java


 
Thread Tools Search this Thread
Top Forums Programming Run Unix commands from Java
# 1  
Old 06-16-2010
Run Unix commands from Java

Greeings all

Im trying to excute a command from Java and direct the output to the main output screen or to another file .... can you please help with this ? can I use filewriter for this ?

Here is my code....

Code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class RunCommand {

    private static boolean str;

    public static void main(String args[]) throws IOException {

        File dir = new File("c:\\Redalert");
        Process proc = Runtime.getRuntime().exec("cmd /c  start dir", null, dir);
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));

          //  FileWriter fw = new FileWriter("c:\\output.txt");

        while (str = stdInput.readLine() != null) {
            System.out.println(str);

        }
    }
}


Last edited by pludi; 06-16-2010 at 05:14 AM..
# 2  
Old 06-16-2010
One way:
Code:
String[] cmd_elements = {"ls", "-l"};

Process prcs = Runtime.getRuntime().exec(cmd_elements);

InputStream cmd_output = prcs.getInputStream();

Process the inputstream to another file
# 3  
Old 06-16-2010
Thank you for the replay, but can you please explain more ?

Im already doing this is in "new InputStreamReader(proc.getInputStream())"

Thanks again
# 4  
Old 06-16-2010
The difference would appear to be the way he's calling exec() there -- giving it an array. It's not a shell -- it's not smart enough to split the string for you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with writing a script to run java commands in sequence in UNIX

Hi, Brand new to these forums, and I hope that someone can help me out. I'm trying to run the following command in UNIX java -jar GenomeAnalysisTK.jar -T SplitSamFile -dt NONE -R reference.fa -I my.bam --outputRoot /my/path/SampleFiles/Sample_ It executes the SplitSamFile from GATK, but I... (3 Replies)
Discussion started by: Wixaros
3 Replies

2. UNIX for Dummies Questions & Answers

How to run UNIX commands on remote machine from windows?

Hi All, I am working in support and we are planning to automate a system to reduce the direct manual intervention to core system. Please find the below details. 1. we have a web application that runs on Windows Platform. 2. From web application, we need to connect to remote Unix machine.... (6 Replies)
Discussion started by: Balaji K
6 Replies

3. Programming

Using basic UNIX commands to make/compile JAVA files

Hello! This is my first post, and I just learned what UNIX was this week. For a JAVA programming class I am taking, I must be able to create a directory in UNIX, use the nano command to create a JAVA program, compile it, and then run it on the command prompt using the java command. For some... (5 Replies)
Discussion started by: UNdvoItX
5 Replies

4. UNIX for Dummies Questions & Answers

How to compile and run java in UNIX?

Hi Im using MobaXterm Unix on my windows XP.I want to compile java in unix.I have installed java to the following path C:\Program Files\Java\jdk1.7.0_09\bin In order to compile the java prog im typing the following command after entering into the bin directory: C:\Program... (2 Replies)
Discussion started by: ak3141
2 Replies

5. UNIX for Dummies Questions & Answers

How to run UNIX commands for practice in Windows 2000 OS?

How to run UNIX commands for practice in Windows 2000 OS? If you suggest to install Cygwin, please let me know the procedure to install, or else I feel happy if you suggest any stand alone app for running UNIX commands..:) (2 Replies)
Discussion started by: Srikanthk
2 Replies

6. UNIX for Advanced & Expert Users

How to execute multiple unix commands in one session from java

Hi, Iam trying to code in java and wanted to run the commands in the Unix remote servers. I have the following code to run multiple GREP commands in a single session. But when i execute this, the first command executes successfully, whereas from the next line it says "Exception Occured... (1 Reply)
Discussion started by: gravi2020
1 Replies

7. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies

8. UNIX for Dummies Questions & Answers

newbie need help to run java in UNIX

I need to run .java from a directory. i.e. c:\aaa\bbb\ccc.java first I compile all the java using javac *.java under the c:\aaa\bbb directory, now I want to run this command under cd .. , Here is the command that I need to type.. java -classpath... (5 Replies)
Discussion started by: uci
5 Replies

9. UNIX for Dummies Questions & Answers

run a java file on UNIX?

HI, I want to run my .java file on a SunOS 5.8 Unix machine. The java file is writen with Ready to Program (Similar to Jbuilder) on a PC. Thanks!! (3 Replies)
Discussion started by: leotopia
3 Replies

10. IP Networking

Run commands on a UNIX with NT

Hi I'm trying to automatically run a command on a UNIX (AIX) machine from a Windows NT 4 machine. I can do this manually using the 'rexec' or 'rsh' command but I need an automatic login (on the AIX). In the manual I've found that there should be a '.rhosts' file on the Unix machine in the... (1 Reply)
Discussion started by: Mark Detrez
1 Replies
Login or Register to Ask a Question