This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to run batch and sh file according to platforms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to run batch and sh file according to platforms" Watch "How to run batch and sh file according to platforms" New topic
Author

How to run batch and sh file according to platforms

Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

Hi All,

Please tell me how i will be able to run a batch file and sh script according to platform.
JDK1.5

please tell me if (windows) run batch elseif unix sh else out not appropriate platform



Regards, Ricky
Oracle Weblogic 10g Certified Expert TechPaste.Com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Please take the time to choose the correct forum for your posts. This forum is for questions on JSP.

For more information, please read this.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Don't go anywhere near Runtime.exec() without reading Michael Daconta's classic article which you can easily find by Googling "when Runtime.exec() won't".

Using .bat and .sh files may mean abandoning platform-independence, so be careful about that.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

I think .sh files are not really executables. Instead, they require a prorgam to run them (usually /bin/sh); you specify this executable as the first line ("!#/bin/sh", "!#/bin/bash", "!#/usr/bin/python", etc). Try changing your command to "sh date.sh" and see if that works.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

Hello All,

My question was from JSP I'm trying to run 2 things 1is a sh file and 2nd is a batch file.
I'm not able to get it done in JSP.
Is someone know how should i proceed.
if windows run batch and if unix run sh.
can some one put some light how should i code it.
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Hi..,

Since the code snippet you've posted is out of a JSP page, I'm not exactly clear as to which platform you are refering to : the server platform [where the webapp is running], or the client platform [from where the client is accessing this weapp].

Anyhow, the generic solution is to identify the OS type, and fire your commands accordingly. Java has certain system level properties set, and one among them is the "os.name" property. So what you can do is:

String osName = System.getProperty("os.name");

if(osName.equals(// check for windows))
{
Runtime.getRuntime().exec(batch_file);
}
else if(osName.equals(// check for linux))
{
Runtime.getRuntime().exec(sh_file);
}
else
{
System.out.println("unsupported OS");
}

Be noted that the sh extension can be understood by other Unix versions too other than Linux... So, code accordingly.
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

hi Thank you Dawn,

I made what you have suggested I am a Fedora 9 user and my module name is like myadmin I am putting the date.sh file inside that
Runtime.getRuntime().exec(application.getRealPath("/date.sh"));
Runtime.getRuntime().exec("date.sh");
seems not working or not also throwing any errors. I f i run through shell its sh file is working.

date.sh just contains
#!/bin/sh
env > abc.txt


What could be thw issue please help

Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

Ok I found it . the problem was with classpath i forgot to put a (dot) before setting the classpath.
now it runs.
One more question??

how to catch the output of the sh file and throw it on screen.

I dont want to put the output to a txt file and read it.

Please help
Thank You
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Check this out:

http://stackoverflow.com/questions/428901/need-to-execute-shell-script-from-webapp-and-display-console-output-in-page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20


Nice link, but the code in it has a flaw. That code does not read anything from the error stream, so it can still block the process (see the article Campbell mentioned).

A little tweaking with how the process is created will easily fix that:
As an advantage, you will now also retrieve the error data.
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

Thank You All

I will try it and let you people know.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to run batch and sh file according to platforms