Solved

Java Code With Unix Code Doubt

Posted on 2011-09-19
3
329 Views
Last Modified: 2013-12-26
Hi Experts...
I have a java program with unix code creating some compiler issues.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      Syntax error on token "", $4}' | sed 's/.$//' >>abc.txt"", delete this token

      at sample.main(sample.java:5)
Please help...
import java.io.*;  
public class sample{  
    public static void main(String[] args){  
	try {  
	    final String[] COMMAND = { "sh", "-c",  "df -k /tmp | tail -1 | nawk -F' ' '{print $NF," ", $4}' | sed 's/.$//' "};
	    Process process=Runtime.getRuntime().exec(COMMAND);
	    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));  
	    String line = null;  
	    while ((line = in.readLine()) != null) {  
		System.out.println(line);  
	    }  
	    in.close();
	} catch (Exception e) {  
	    System.out.println(e);  
	}  
    }
}

Open in new window

0
Comment
Question by:gaugeta
  • 2
3 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36560823
try replacing line 5

final String[] COMMAND = { "sh", "-c",  "df -k /tmp | tail -1 | nawk -F", "'{print $NF',' $4}' | sed 's/.$//' "};
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36560826

You need to escape double quotes within the unix code:

 final String[] COMMAND = { "sh", "-c",  "df -k /tmp | tail -1 | nawk -F' ' '{print $NF,\" \", $4}' | sed 's/.$//' "};

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36560844
Otherwise it understand double quotes within your unix command as end iof te argument in the COMMAND string but sees no comma before the next argument
so " inside you Unix command should be changed to \"
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

Suggested Solutions

Title # Comments Views Activity
Facing this issue for maven proxy setting 2 27
dao vs facade design patterns 2 38
JDBC performance 8 29
Coding for the first time 9 70
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

730 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