Solved

Java Code With Unix Code Doubt

Posted on 2011-09-19
3
330 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: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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

688 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