954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Executing dos commands in Java

Hi,
I want to execute unix commands(pdftops and ps2pdf) in windows system using java environment. but m getting error.plz help me.
here is my code:

public static  Process p1;
public static Runtime rt;
 rt = Runtime.getRuntime();
            ////linux commands
            filename=args[0].replace(".pdf","temp.pdf");
            String pdftopsCmd ="pdftops "+args[0]+" temp.ps";
            String pstopdfCmd="ps2pdf temp.ps " +filename;
            p1 = rt.exec(pdftopsCmd);//p1=process
            Thread.sleep(10000);
            p1=rt.exec(pstopdfCmd);

and the error is :

Exception in thread "main" java.io.IOException: CreateProcess: pdftops Test2.pdf temp.ps error=2
	at java.lang.ProcessImpl.create(Native Method)
	at java.lang.ProcessImpl.<init>(Unknown Source)
	at java.lang.ProcessImpl.start(Unknown Source)
	at java.lang.ProcessBuilder.start(Unknown Source)
	at java.lang.Runtime.exec(Unknown Source)
	at java.lang.Runtime.exec(Unknown Source)
	at java.lang.Runtime.exec(Unknown Source)
ainct
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Is that maybe because Unix commands don't execute when you aren't in a Unix environment.. ?

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

Hi, I want to execute unix commands(pdftops and ps2pdf) in windows system using java environment. but m getting error.plz help me. here is my code:

public static  Process p1;
public static Runtime rt;
 rt = Runtime.getRuntime();
            ////linux commands
            filename=args[0].replace(".pdf","temp.pdf");
            String pdftopsCmd ="pdftops "+args[0]+" temp.ps";
            String pstopdfCmd="ps2pdf temp.ps " +filename;
            p1 = rt.exec(pdftopsCmd);//p1=process
            Thread.sleep(10000);
            p1=rt.exec(pstopdfCmd);

and the error is :

Exception in thread "main" java.io.IOException: CreateProcess: pdftops Test2.pdf temp.ps error=2
	at java.lang.ProcessImpl.create(Native Method)
	at java.lang.ProcessImpl.<init>(Unknown Source)
	at java.lang.ProcessImpl.start(Unknown Source)
	at java.lang.ProcessBuilder.start(Unknown Source)
	at java.lang.Runtime.exec(Unknown Source)
	at java.lang.Runtime.exec(Unknown Source)
	at java.lang.Runtime.exec(Unknown Source)


There are tools available such as Cygwin which gives linux environment in the Windows. First install it and then try to execute commands on it.

cute.sridhar
Newbie Poster
6 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
Is that maybe because Unix commands don't execute when you aren't in a Unix environment.. ?


I too don't understand this concept but my TL is telling that it's possible. This is like a nightmare to me. Don't know how to resolve.

ainct
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

First learn the difference between DOS, Windows, and Unix.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Hi~ I'am S.Korean .Your coding is very good...but..like ~ below...
...
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
..

sang sang su
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Is this what you are looking for?

thomas_naveen
Junior Poster
168 posts since Jan 2010
Reputation Points: 136
Solved Threads: 36
 

Thank you navin, I tried that link before but of no use (Thanks sridhar for suggesting the tool )As of now i am just compiling the java file in windows m/c and executing in Linux m/c. The code that i have provided very well execute in linux m/c. Any1 of u got any clue (how to run on windows) then please let me know.

ainct
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You