0

In my war, I have file exe in WEB-INF\classes\

How can I execute this file in Java code (How can I specify path to this file) ?

command = " ? ";
Process x = p.exec(command);
Anvesh Saxena
  • 4,458
  • 2
  • 23
  • 30
user2519456
  • 21
  • 1
  • 5

3 Answers3

1

Te following approach could work:

1) Prepare full path of your executable:

ServletContext context = getContext();
String fullPath = context.getRealPath("/WEB-INF/classes/executable");

2) Execute like you would normally do it:

String[] cmd = { fullPath /*[...] arguments */};
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();

This is a simplified example; you may also want to read more about ProcessBuilder.

Art Licis
  • 3,619
  • 1
  • 29
  • 49
  • ok, thanks, but I have new other problems, resolved in another way – user2519456 Jun 25 '13 at 12:14
  • Depends on where you use it and how. If that's a test server or predefined infrastructure, and this is an easy way to automate (maybe temporal solution) -- why not. Otherwise I would agree that 'it's a bad idea' and you should consider moving platform-specific executables out of war. – Art Licis Jun 25 '13 at 12:21
1

This is bad idea. Imagine simply fact that your .war packege should run on almost any server (".war is platform independend") and your .exe file is compiled just for one architecture.

Better should be execute your .exe as external program just for separate platform independent and platform dependent part. Then in java you can test operating system and on this basis run desired externel programm.

Read this link with similar question.

Community
  • 1
  • 1
1ac0
  • 2,875
  • 3
  • 33
  • 47
0

The best way to do find a file's real location inside a web app is to use the ServletContext.getRealPath (see http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String))

You can access that object from the session...

albervera
  • 45
  • 7