This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes how to execute commands in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to execute commands in servlets" Watch "how to execute commands in servlets" New topic
Author

how to execute commands in servlets

tinnu maverick
Greenhorn

Joined: Oct 06, 2010
Posts: 2
I have an ingest command which i need to run to ingest objects into fedora repository.
The command is

/srv/fedora/client/bin/fedora-ingest.sh f SIUImages_bike.xml info:fedora/fedora-system:FOXML-1.1 test.lib.siu.edu:8080 fedoraAdmin fedoraAdmin http

I want to execute this command in a servlet...

I had tried

try {
Runtime rt = Runtime.getRuntime();

Process pr = rt.exec("/srv/fedora/client/bin/fedora-ingest.sh f SIUImages_bike.xml info:fedora/fedora-system:FOXML-1.1 test.lib.siu.edu:8080 fedoraAdmin fedoraAdmin http");

BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String line=null;

while((line=input.readLine()) != null) {
out.println(line);

}

int exitVal = pr.waitFor();
out.println("This is Exited with error code "+exitVal);

}
catch(Exception e) {
out.println(e.toString());
e.printStackTrace();
}

I had tried this but the command was not executed successfully. It showed me a message "This is Exited with error code 0".

My question is

the file(SIUImages_bike.xml) which i am using in the command is located at path "/srv/fedora/tomcat/webapps/formC/WEB-INF/classes/FOXML"

now i want to use
Process pr = rt.exec(" FILE PATH HERE /srv/fedora/client/bin/fedora-ingest.sh f SIUImages_bike.xml info:fedora/fedora-system:FOXML-1.1 test.lib.siu.edu:8080 fedoraAdmin fedoraAdmin http");

but i already have a "srv" in the command
Process pr = rt.exec(" /srv/fedora/tomcat/webapps/formC/WEB-INF/classes/FOXML /srv/fedora/client/bin/fedora-ingest.sh f SIUImages_bike.xml info:fedora/fedora-system:FOXML-1.1 test.lib.siu.edu:8080 fedoraAdmin fedoraAdmin http;

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Welcome to JavaRanch! Please => UseCodeTags when you post a code snap!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Actually, an exit status of 0 means that the command was executed successfully. If the command isn't producing the results you are expecting, then maybe there is something wrong with your command syntax or with your shell script.

Try using the absolute path of "SIUImages_bike.xml" instead of just its file name.


SCJP 6 || SCWCD 5
tinnu maverick
Greenhorn

Joined: Oct 06, 2010
Posts: 2
Michael Angstadt wrote:Actually, an exit status of 0 means that the command was executed successfully. If the command isn't producing the results you are expecting, then maybe there is something wrong with your command syntax or with your shell script.

Try using the absolute path of "SIUImages_bike.xml" instead of just its file name.



Thank you so much Michael Angstadt!! I had forgotten to give the absolute path while executing the command. Thanks again!! Gained new energy after solving this problem!!
 
Consider Paul's rocket mass heater.
 
subject: how to execute commands in servlets
 
Similar Threads
Runtime.getRuntime().exec(commandToExecute) without promting for enter key to be pressed
run exe in client sid
Running external application within Java application
Reg:Executing Commands
Execute sqlldr command from java code.