• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to run batch and sh file according to platforms

 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You All

I will try it and let you people know.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic