how to use shell program parameters in java program?

Hai all,

I am writing a java program which must read strings from a file and must assign to variables.these strings are nothing but the output of ls -l command in shell. i need to have all those 9 fields in a file as variables.

I cant find any method that will read only string.I came accross read line but that is not what i need.So, i thought of writing a shell script and by using awk, storing those 9 fields in 9 variables.. but how to use that variables in java programming?

the shell script is executed by exec() but i need that variables also.

Help me please:(

thanks in advance

[622 byte] By [sujjia] at [2007-10-3 2:28:24]
# 1
use readLine(), which you have already found, then split() the string.Not everything is spoon-fed to you from the API, but this one gets as close as you can get.
masijade.a at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 2

when i am the split command i am getting any error while compiling but after execution,this is what happens

Exception in thread "main" java.lang.NullPointerException

at Compare.main(java.lang.String[]) (Unknown Source)

at gnu.java.lang.MainThread.call_main() (/usr/lib/libgcj.so.6.0.0)

at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)

if i hides that line then the code works fine:(

sujjia at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 3

> when i am the split command i am getting any error

> while compiling but after execution,this is what

> happens

>

> Exception in thread "main"

> java.lang.NullPointerException

So what? Just because you programmed a bug it doesn't mean the concept doesn't work. I don't know how to fix it, since you obviously don't want us to see your code.

CeciNEstPasUnProgrammeura at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 4
Show your split line (and a few lines on either side) and provide the output of a java -version command.And if this is linux then also provide the output of the following commandrpm -qa | egrep "java|jdk|j2dk|sdk|jre|j2se"
masijade.a at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 5

o/p if rpm -qa | egrep "java|jdk|j2dk|sdk|jre|j2se"

java-1.4.2-gcj-compat-1.4.2.0-40jpp_31rh

jre-1.5.0_06-fcs

gnu-crypto-jce-jdk1.4-2.0.1-1jpp_5fc

j2sdk-1.4.2_12-fcs

gnu-crypto-sasl-jdk1.4-2.0.1-1jpp_5fc

o/p of java -version is:

java version "1.4.2"

gij (GNU libgcj) version 4.0.0 20050519 (Red Hat 4.0.0-8)

Copyright (C) 2005 Free Software Foundation, Inc.

This is free software; see the source for copying conditions. There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

my code is...

p is a string,a is an array of strings

ip = new BufferedReader(new FileReader("op.txt"));

p=ip.readLine();

a=p.split("\\s");

// i gave \\s since i read from java doc that u must give for that for spaces.eventhouh i am not using it i am getting the same error...

i am not saying that the concept is wrong.. but i want to know y that error came, and want to modify it.

thank you

sujjia at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 6

if you read the API documentation, you will see that readline() returns a null, when it has reached the end of the stream. Which will explain your NullPointerException. You need to check for this condition.

Sorry about the rpm stuff, it's just that the gnu.java through me for a loop. I hate those gcj-compat java packages. I don't know why, I just do.

masijade.a at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 7

can we use the same file for reading and writing at the same time?

i think this is where i went wrong.But what i need is.. one class creates a file and writes the shell output in that.Now in main i want to read that file and split that.But this is all done in a loop.

I mean.. i took both filereader and fileoutputstream with same txt file...

i am newbie.. so, please do understand me with patience..

thank you

sujjia at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 8

You would be better served processing the output stream of a Runtime.exec() directly, rather than writing to a file initially.

But to answer the question, with the way you are doing it, no, you cannot read and write to the same file concurrently. With RandamAccessFile, it would be possible, but I do not reccommend this.

masijade.a at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 9

i now changed the code... and closed the output file and then i read.Later i splited with the line i read.. but the same happened...

this is the code:

while ((l = inputStream.readLine()) != null)

{

op=new FileOutputStream("op.txt");//output of exec goes to this file

String[] cmd= {"/bin/sh","-c","ls -l "+l};

rt = Runtime.getRuntime();

proc = rt.exec(cmd);

// any error message?

StreamGobbler errorGobbler = new

StreamGobbler(proc.getErrorStream(), "ERR");

// any output?

StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "",op);

errorGobbler.start();

outputGobbler.start();

op.close();//the file is closed now

ip = new BufferedReader(new FileReader("op.txt"));//now i opened to read

p=ip.readLine();

a=p.split("\\s");

ip.close();

}

where i went wrong?

sujjia at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 10

Well, first off you should use proc.waitFor() before op.close() to ensure that the process finishes running. You may have closed the file before anything was written to it.

Also, writing to and reading from the file are extra steps you do not need to do. Use the output stream from proc directly, rather than writing the output to a file.

masijade.a at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 11
u mean without having input and output streams?i have a class that extends thread.This will write to the output file.And i want to access that from main.So, i kept that in a file.
sujjia at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...
# 12
Are you processing the output of "ls -l" because it gives you something that the plain ordinary java.io.File object doesn't, or because you didn't know about java.io.File? If it's the latter, I would recommend throwing away all that code and rewriting.
DrClapa at 2007-7-14 19:27:28 > top of Java-index,Java Essentials,New To Java...