running unix command from java


 
Thread Tools Search this Thread
# 8  
@jlliagre

yes. it does clear the file. BUT only when i run from the unix console.
When the same is attempted from a java programme, it fails throwing at the earlier mentioned error.

---------- Post updated at 06:03 PM ---------- Previous update was at 05:53 PM ----------

@Peasant

I still get the same error Smilie

Code:
 ksh: cp /dev/null /t3/envs/dmdev3/test/file_list.txt:  not found

# 9  
Quote:
ksh: cp /dev/null /t3/envs/dmdev3/test/file_list.txt: not found
This error message probably came from the following command:
Code:
ksh 'cp /dev/null /t3/envs/dmdev3/test/file_list.txt'
Or even just:
'cp /dev/null /t3/envs/dmdev3/test/file_list.txt'

i.e. It tries to execute a command with the name of all the characters between the single quotes including the space characters.
Just for interest, this would have worked:
Code:
ksh -c 'cp /dev/null /t3/envs/dmdev3/test/file_list.txt'

Something is going wrong with the quote characters on the ssh command line. I suggest placing the whole command in a Shell script, and then calling that Shell script from java.


Did we see the asignment of strShellCmd?
# 10  
It looks like you are trying to execute quoted commands:
"cp /dev/null /t3/envs/dmdev3/test/file_list.txt" or ":> /t3/envs/dmdev3/test/file_list.txt" instead of these unquoted ones, eg: : > /dev/null /t3/envs/dmdev3/test/file_list.txt.
# 11  
@all

i have found out the solution. Though the shell command was right, the way i called it from JAVA caused the issue. The correct java code would be as shown below.

Code:
ProcessBuilder procBuild=new ProcessBuilder("bash","-c",strShellCmd);

This did the trick and now it works without a flaw for ANY SHELL COMMAND. SmilieSmilie

Thanks all,
Madhu.
# 12  
Thank you for posting the solution.

While Googling I came across similar references, but they cross-linked to the old Sun Java site which Oracle in their wisdom have restructured such that old links don't work.

The problem is that the Java exec command tokenizes the command call and places each token onto the system call in order.
Code:
Thus:
exec(command)
is the same as
exec(command, null, null)

I'd still place the unix command in a Shell script (complete with appropriate Shebang line) because I can see potential for problems with quote characters and accidentally providing switches to the Shell command.
# 13  
@methyl

yeah you are right about the tokenizing part. Also, bash command is requried as the primary parameter while running commands with more than two arguments.

Anyways can you please elaborate your last statement?

Quote:
I'd still place the unix command in a Shell script (complete with appropriate Shebang line) because I can see potential for problems with quote characters and accidentally providing switches to the Shell command.
PS : Can you please tell me how to close/resolve this thread?

Thanks,
Madhu.

Moderator's Comments:
Mod Comment Thread closed as requested

Last edited by methyl; 07-09-2012 at 01:11 PM..
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Computers
Difficulty: Easy
Linux was first created as an alternative to Windows XP.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command to get exact tomcat process I am running ignoring other java process

Team, I have multiple batchjobs running in VM, if I do ps -ef |grep java or tomcat I am getting multiple process list. How do I get my exact tomcat process running and that is unique? via shell script? (4 Replies)
Discussion started by: Ghanshyam Ratho
4 Replies

2. UNIX for Advanced & Expert Users

Command to check java file in UNIX

Guys, i need to check whether java file can be executed in my current unix system or not. Can anyone please help me to get the correct command to check this. My Unix system is HP-UX. Thanks, (1 Reply)
Discussion started by: AraR87
1 Replies

3. UNIX for Advanced & Expert Users

running unix command from java

Hi All, I have been struggling for a week trying to run a unix command from my java program. the unix command is = ssh dmdev3@tsapid01-zt3d01 ':> /t3/envs/dmdev3/test/file_list.txt' when i try to run this command directly on my unix console, it works perfectly. but when i try it form my... (0 Replies)
Discussion started by: madhu_sharan
0 Replies

4. Programming

Exception Signal 11 while running JAVA code in UNIX

Hi, when i compile my java code in UNIX using javac, the class file is getting created. But when i try to run the code using java - classpath command, i get the following error. ---------------------------------------- /u/up11/sample/request:>java -classpath /u/up11/sample/request... (0 Replies)
Discussion started by: satish2712
0 Replies

5. Programming

execute unix command from java eclips

Hi all, I tried to execute the command by 2 ways : 1) Java Runtime class exec method - but the problem there is that it only can execute scripts without arguments(I need with args) 2) the Jtux classs Uprocess- the problem it is not supported by windows and my eclips platform is on... (0 Replies)
Discussion started by: katzs500
0 Replies

6. Programming

exit status running java classpath in unix shell

I have a java classpath running inside of a unix shell script. During my testing it will error with lines that show an example like this below. java.io.FileNotFoundException error at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:129), ... (2 Replies)
Discussion started by: mmcds
2 Replies

7. Shell Programming and Scripting

empty space in java command through unix

I need to pass an empty space in the command ="java ......... " as one of the arguments where java calls some java command.... (It accepts it in windows as " ") Thanks in advance SD (2 Replies)
Discussion started by: shweta_d
2 Replies

8. Shell Programming and Scripting

execute command unix with java:possible or not?

hi everybody, i have a small unix script and i want to use it with java, so how can i executte unix command with java (if it is possible): in fact i want to use some values returned by this script with java swings. is it possible (of course everything is possible) but how can i do? thank you in... (4 Replies)
Discussion started by: mips
4 Replies

9. UNIX for Dummies Questions & Answers

Executing UNIX command from java on NT

Hi - I am totally new to UNIX so please bear with me... I run a java program on Win NT server to do file ftp to UNIX server. I log in, cd, create ftp file on UNIX and quit from my java progam - all works well. Now I want to execute a script on UNIX. At the UNIX 'console'/'shell' (!?) you... (1 Reply)
Discussion started by: CJ Walt
1 Replies

10. UNIX for Dummies Questions & Answers

Java program calling a UNIX command

I was wondering if it was possible to call a unix command directly from a Java program during run-time. This command could very very basic e.g. "ps -ef" returned as a string, all I need is a starting place so if anyone has any suggestion or examples I would be very grateful (2 Replies)
Discussion started by: QUartz Ite
2 Replies

Featured Tech Videos