Solved

Java Code With Unix Code Doubt

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now