0

Hi I am creating a rest api which returns the details of an executable file as response. My requirement is if i send a request to start an executable file,the response should show the details of the executable file that it is started. In my code am giving a path where the details of the executable file are also stored in a .txt file.But if am giving the path of the .txt file in my code to store the details the rest api is not returning any response. If I remove the path of the .txt file the response is given as expected.Any ideas as where am going wrong.below is the code snippet. The code to save the details in a.txt file is

              *****d[+] C:\\Debug\\logfile.txt*****

javacode:

public String startServer() throws IOException, InterruptedException{

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("C:\\test.exe -d[+] C:\\Debug\\logfile.txt");
    BufferedReader input = new BufferedReader(new InputStreamReader 
(pr.getInputStream()));

String line=null;
StringBuffer start= new StringBuffer();
    while((line=input.readLine()) != null) {
        start.append(line + "\n");
        System.out.println(line);
}

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



return start.toString();
}
}

Webservice:

@Path("/server")
public class LicenseServer {

@GET
@Path("start")
@Produces({"text/plain","application/xml","application/json"})
//@Consumes({ "application/xml", "application/json", "application/x-www-form-  
urlencoded" })
public String getServer() throws IOException, InterruptedException{
    StartServer start = new StartServer();
    return start.startServer();

}
user2821894
  • 988
  • 2
  • 11
  • 20
  • 1
    This has nothing to do with REST. Try to make `startServer` work. –  Oct 03 '13 at 16:53
  • @Tichodroma: Thanks for the quick reply. startServer does work if I remove -d[+] C:\\Debug\\logfile.txt from Process pr = rt.exec("C:\\test.exe -d[+] C:\\Debug\\logfile.txt"); – user2821894 Oct 03 '13 at 16:59
  • So make it work *with* this path. This is a problem of how `Runtime.exec` interprets its argument. –  Oct 03 '13 at 17:00
  • @Tichodroma:It does work with this path and when I open the .txt file from the specified path am able to see the details in it. The problem is the response page does show the response or does not display anything on the page. – user2821894 Oct 03 '13 at 17:03
  • You're probably going to want to use proper quoting for that exec string. Otherwise the space before `-d[+]` will cause problems. – Justin Jasmann Oct 03 '13 at 17:23
  • @JustinJasmann: any suggestions as to where i can look for proper quoting for these kind of strings. – user2821894 Oct 03 '13 at 17:27
  • @user2821894 http://stackoverflow.com/questions/4916918/java-execute-a-command-with-a-space-in-the-pathname – Justin Jasmann Oct 03 '13 at 17:34
  • @JustinJasmann:Hi Justin thanks for sharing above link. I modified my code with processbuilder as mentioned in above link instead of exec but still it works the same as before. – user2821894 Oct 03 '13 at 17:54
  • @user2821894 When you debug the REST service, do you see anything obvious? Does `text.exe` expect these parameters? Is it actually running with these parameters? – Justin Jasmann Oct 03 '13 at 17:58
  • @JustinJasmann:yes it is running with these parameters. My issue is it does not show the response in the uri. Suppose my uri is http://localhost:8080/project/server/start. This page should contain the response. but it is blank. – user2821894 Oct 03 '13 at 18:03

0 Answers0