[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java Code With Unix Code Doubt

Posted on 2011-09-19
3
Medium Priority
?
344 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:Gurvinder Pal Singh
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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 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