java execute script in Tomcat












0















I created a spring boot project, which I'm running on a local tomcat (I'm planning to deploy this to a webserver). Within this project I created a rest-service, which should execute a .bat file.



my rest services look like this (neither of them works)



@RequestMapping(value = "/esc", method= RequestMethod.GET)
public String esc() throws IOException, InterruptedException {
String folder = "P:\Documents\testcmd";
String cmdarray = new String{"cmd -c","dosomething.cmd"};
ProcessBuilder processBuilder = new ProcessBuilder( cmdarray );
processBuilder.directory(new File(folder));
Process process = processBuilder.start();

int exitCode = -1;
boolean finished = false;
while ( !finished ) {
exitCode = process.waitFor();
finished = true;
}
return folder;
}

@RequestMapping(value = "/ex", method= RequestMethod.GET)
public String executeShellScript(){
//final String shCmd = "/bin/bash -c helloworld.sh";
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
final String shCmd = "cmd -c P:/Documents/testcmd/dosomething.cmd";
String output = executeCommand(shCmd);

return output;
}
private String executeCommand(String command){
Process p;
InputStream in = null;
String value = "";
try {
p = Runtime.getRuntime().exec(command);
in = p.getInputStream();
int ch;
while((ch = in.read()) != -1) {
value = String.valueOf((char)ch);
}
}catch (IOException e){
e.printStackTrace();
}
return value;
}


I tried it with a processbuilder and with runtime.
The file I want to execute is in this folder: "P:Documentstestcmd"



Is it even possible to execute a local file with a tomcat server?










share|improve this question


















  • 1





    Are you running on Windows or Linux ? (it's unclear as you call /bin/bash and cmd -c )

    – Eugène Adell
    Nov 13 '18 at 16:23











  • i am running this on windows

    – vZ_LiTe
    Nov 14 '18 at 6:36
















0















I created a spring boot project, which I'm running on a local tomcat (I'm planning to deploy this to a webserver). Within this project I created a rest-service, which should execute a .bat file.



my rest services look like this (neither of them works)



@RequestMapping(value = "/esc", method= RequestMethod.GET)
public String esc() throws IOException, InterruptedException {
String folder = "P:\Documents\testcmd";
String cmdarray = new String{"cmd -c","dosomething.cmd"};
ProcessBuilder processBuilder = new ProcessBuilder( cmdarray );
processBuilder.directory(new File(folder));
Process process = processBuilder.start();

int exitCode = -1;
boolean finished = false;
while ( !finished ) {
exitCode = process.waitFor();
finished = true;
}
return folder;
}

@RequestMapping(value = "/ex", method= RequestMethod.GET)
public String executeShellScript(){
//final String shCmd = "/bin/bash -c helloworld.sh";
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
final String shCmd = "cmd -c P:/Documents/testcmd/dosomething.cmd";
String output = executeCommand(shCmd);

return output;
}
private String executeCommand(String command){
Process p;
InputStream in = null;
String value = "";
try {
p = Runtime.getRuntime().exec(command);
in = p.getInputStream();
int ch;
while((ch = in.read()) != -1) {
value = String.valueOf((char)ch);
}
}catch (IOException e){
e.printStackTrace();
}
return value;
}


I tried it with a processbuilder and with runtime.
The file I want to execute is in this folder: "P:Documentstestcmd"



Is it even possible to execute a local file with a tomcat server?










share|improve this question


















  • 1





    Are you running on Windows or Linux ? (it's unclear as you call /bin/bash and cmd -c )

    – Eugène Adell
    Nov 13 '18 at 16:23











  • i am running this on windows

    – vZ_LiTe
    Nov 14 '18 at 6:36














0












0








0








I created a spring boot project, which I'm running on a local tomcat (I'm planning to deploy this to a webserver). Within this project I created a rest-service, which should execute a .bat file.



my rest services look like this (neither of them works)



@RequestMapping(value = "/esc", method= RequestMethod.GET)
public String esc() throws IOException, InterruptedException {
String folder = "P:\Documents\testcmd";
String cmdarray = new String{"cmd -c","dosomething.cmd"};
ProcessBuilder processBuilder = new ProcessBuilder( cmdarray );
processBuilder.directory(new File(folder));
Process process = processBuilder.start();

int exitCode = -1;
boolean finished = false;
while ( !finished ) {
exitCode = process.waitFor();
finished = true;
}
return folder;
}

@RequestMapping(value = "/ex", method= RequestMethod.GET)
public String executeShellScript(){
//final String shCmd = "/bin/bash -c helloworld.sh";
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
final String shCmd = "cmd -c P:/Documents/testcmd/dosomething.cmd";
String output = executeCommand(shCmd);

return output;
}
private String executeCommand(String command){
Process p;
InputStream in = null;
String value = "";
try {
p = Runtime.getRuntime().exec(command);
in = p.getInputStream();
int ch;
while((ch = in.read()) != -1) {
value = String.valueOf((char)ch);
}
}catch (IOException e){
e.printStackTrace();
}
return value;
}


I tried it with a processbuilder and with runtime.
The file I want to execute is in this folder: "P:Documentstestcmd"



Is it even possible to execute a local file with a tomcat server?










share|improve this question














I created a spring boot project, which I'm running on a local tomcat (I'm planning to deploy this to a webserver). Within this project I created a rest-service, which should execute a .bat file.



my rest services look like this (neither of them works)



@RequestMapping(value = "/esc", method= RequestMethod.GET)
public String esc() throws IOException, InterruptedException {
String folder = "P:\Documents\testcmd";
String cmdarray = new String{"cmd -c","dosomething.cmd"};
ProcessBuilder processBuilder = new ProcessBuilder( cmdarray );
processBuilder.directory(new File(folder));
Process process = processBuilder.start();

int exitCode = -1;
boolean finished = false;
while ( !finished ) {
exitCode = process.waitFor();
finished = true;
}
return folder;
}

@RequestMapping(value = "/ex", method= RequestMethod.GET)
public String executeShellScript(){
//final String shCmd = "/bin/bash -c helloworld.sh";
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
final String shCmd = "cmd -c P:/Documents/testcmd/dosomething.cmd";
String output = executeCommand(shCmd);

return output;
}
private String executeCommand(String command){
Process p;
InputStream in = null;
String value = "";
try {
p = Runtime.getRuntime().exec(command);
in = p.getInputStream();
int ch;
while((ch = in.read()) != -1) {
value = String.valueOf((char)ch);
}
}catch (IOException e){
e.printStackTrace();
}
return value;
}


I tried it with a processbuilder and with runtime.
The file I want to execute is in this folder: "P:Documentstestcmd"



Is it even possible to execute a local file with a tomcat server?







java spring rest shell tomcat






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 13:00









vZ_LiTevZ_LiTe

194




194








  • 1





    Are you running on Windows or Linux ? (it's unclear as you call /bin/bash and cmd -c )

    – Eugène Adell
    Nov 13 '18 at 16:23











  • i am running this on windows

    – vZ_LiTe
    Nov 14 '18 at 6:36














  • 1





    Are you running on Windows or Linux ? (it's unclear as you call /bin/bash and cmd -c )

    – Eugène Adell
    Nov 13 '18 at 16:23











  • i am running this on windows

    – vZ_LiTe
    Nov 14 '18 at 6:36








1




1





Are you running on Windows or Linux ? (it's unclear as you call /bin/bash and cmd -c )

– Eugène Adell
Nov 13 '18 at 16:23





Are you running on Windows or Linux ? (it's unclear as you call /bin/bash and cmd -c )

– Eugène Adell
Nov 13 '18 at 16:23













i am running this on windows

– vZ_LiTe
Nov 14 '18 at 6:36





i am running this on windows

– vZ_LiTe
Nov 14 '18 at 6:36












1 Answer
1






active

oldest

votes


















1














I solved the problem. The sollution with Runtime.getRuntime().exec(command) was correct. Only my system call was wrong. Instead of final String shCmd = "cmd -c P:/Documents/testcmd/dosomething.cmd"; I had to use "P:/Documents/testcmd/dosomething.cmd". In addition I had to change the dosomething.cmd because it was wrong. When executing the plain java code, the file would open a cmd terminal and then print hello in an endless loop. I changed the file content that instead of the endless loop in the terminal it prints hello into another file.



// method is mapped on root/ex
@RequestMapping(value = "/ex", method= RequestMethod.GET)
public String executeShellScript(){
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
final String shCmd = "P:\Documents\testcmd\dosomething.cmd -c";
String output = executeCommand(shCmd);

return output;
}


batch file before & after



@echo off
:start
echo hallo
pause
goto start


after



@echo off
@echo This is a test>> P:/Documents/testcmd/file.txt
@echo 123>> P:/Documents/testcmd/file.txt
@echo 245.67>> P:/Documents/testcmd/file.txt





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281578%2fjava-execute-script-in-tomcat%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I solved the problem. The sollution with Runtime.getRuntime().exec(command) was correct. Only my system call was wrong. Instead of final String shCmd = "cmd -c P:/Documents/testcmd/dosomething.cmd"; I had to use "P:/Documents/testcmd/dosomething.cmd". In addition I had to change the dosomething.cmd because it was wrong. When executing the plain java code, the file would open a cmd terminal and then print hello in an endless loop. I changed the file content that instead of the endless loop in the terminal it prints hello into another file.



    // method is mapped on root/ex
    @RequestMapping(value = "/ex", method= RequestMethod.GET)
    public String executeShellScript(){
    System.out.println("Working Directory = " +
    System.getProperty("user.dir"));
    final String shCmd = "P:\Documents\testcmd\dosomething.cmd -c";
    String output = executeCommand(shCmd);

    return output;
    }


    batch file before & after



    @echo off
    :start
    echo hallo
    pause
    goto start


    after



    @echo off
    @echo This is a test>> P:/Documents/testcmd/file.txt
    @echo 123>> P:/Documents/testcmd/file.txt
    @echo 245.67>> P:/Documents/testcmd/file.txt





    share|improve this answer




























      1














      I solved the problem. The sollution with Runtime.getRuntime().exec(command) was correct. Only my system call was wrong. Instead of final String shCmd = "cmd -c P:/Documents/testcmd/dosomething.cmd"; I had to use "P:/Documents/testcmd/dosomething.cmd". In addition I had to change the dosomething.cmd because it was wrong. When executing the plain java code, the file would open a cmd terminal and then print hello in an endless loop. I changed the file content that instead of the endless loop in the terminal it prints hello into another file.



      // method is mapped on root/ex
      @RequestMapping(value = "/ex", method= RequestMethod.GET)
      public String executeShellScript(){
      System.out.println("Working Directory = " +
      System.getProperty("user.dir"));
      final String shCmd = "P:\Documents\testcmd\dosomething.cmd -c";
      String output = executeCommand(shCmd);

      return output;
      }


      batch file before & after



      @echo off
      :start
      echo hallo
      pause
      goto start


      after



      @echo off
      @echo This is a test>> P:/Documents/testcmd/file.txt
      @echo 123>> P:/Documents/testcmd/file.txt
      @echo 245.67>> P:/Documents/testcmd/file.txt





      share|improve this answer


























        1












        1








        1







        I solved the problem. The sollution with Runtime.getRuntime().exec(command) was correct. Only my system call was wrong. Instead of final String shCmd = "cmd -c P:/Documents/testcmd/dosomething.cmd"; I had to use "P:/Documents/testcmd/dosomething.cmd". In addition I had to change the dosomething.cmd because it was wrong. When executing the plain java code, the file would open a cmd terminal and then print hello in an endless loop. I changed the file content that instead of the endless loop in the terminal it prints hello into another file.



        // method is mapped on root/ex
        @RequestMapping(value = "/ex", method= RequestMethod.GET)
        public String executeShellScript(){
        System.out.println("Working Directory = " +
        System.getProperty("user.dir"));
        final String shCmd = "P:\Documents\testcmd\dosomething.cmd -c";
        String output = executeCommand(shCmd);

        return output;
        }


        batch file before & after



        @echo off
        :start
        echo hallo
        pause
        goto start


        after



        @echo off
        @echo This is a test>> P:/Documents/testcmd/file.txt
        @echo 123>> P:/Documents/testcmd/file.txt
        @echo 245.67>> P:/Documents/testcmd/file.txt





        share|improve this answer













        I solved the problem. The sollution with Runtime.getRuntime().exec(command) was correct. Only my system call was wrong. Instead of final String shCmd = "cmd -c P:/Documents/testcmd/dosomething.cmd"; I had to use "P:/Documents/testcmd/dosomething.cmd". In addition I had to change the dosomething.cmd because it was wrong. When executing the plain java code, the file would open a cmd terminal and then print hello in an endless loop. I changed the file content that instead of the endless loop in the terminal it prints hello into another file.



        // method is mapped on root/ex
        @RequestMapping(value = "/ex", method= RequestMethod.GET)
        public String executeShellScript(){
        System.out.println("Working Directory = " +
        System.getProperty("user.dir"));
        final String shCmd = "P:\Documents\testcmd\dosomething.cmd -c";
        String output = executeCommand(shCmd);

        return output;
        }


        batch file before & after



        @echo off
        :start
        echo hallo
        pause
        goto start


        after



        @echo off
        @echo This is a test>> P:/Documents/testcmd/file.txt
        @echo 123>> P:/Documents/testcmd/file.txt
        @echo 245.67>> P:/Documents/testcmd/file.txt






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 10:23









        vZ_LiTevZ_LiTe

        194




        194






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281578%2fjava-execute-script-in-tomcat%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python