?
Solved

Java Code With Unix Code Doubt

Posted on 2011-09-19
3
Medium Priority
?
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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…
Utilizing an array to gracefully append to a list of EmailAddresses
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 Month11 days, 10 hours left to enroll

752 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