How to run following CMD code throgh java class?

283 views Asked by At

I'm starting Fuseki server through cmd line interface command. Following way;

    ![My CMD working code]

enter image description here

    D:
    cd jena-fuseki-1.0.1
    fuseki-server --update --mem /ds

How can I run above code through java class without opening any CMD line interface? So many stackoverflow answers provide below code.

     Process child = Runtime.getRuntime().exec(command);

But I didn't get how should apply my above things to here.

Updated question;

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String[] command = { "fuseki-server", "--update", "--mem", "/ds" };
    File directory = new File("D:\\jena-fuseki-1.0.1");
    ProcessBuilder pb = new ProcessBuilder(command);
    pb.directory(directory);

    try {
        pb.start();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

} Error message : "java.io.IOException: Cannot run program "fuseki-server" (in directory "D:\jena-fuseki-1.0.1"): CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(Unknown Source)"

enter image description here

2

There are 2 answers

5
Teudimundo On BEST ANSWER

You need to put the extension (.exe, .bat, ...) to the fuseki-server command file (the shell does it for you but here you are giving the filename).

You also need to use pb.start() to actually start the process.

2
bmargulies On

Fuseki is a java servlet webapp. See https://github.com/apache/jena/tree/master/jena-fuseki2/src/main/webapp. You can take the .war file and put it in tomcat or jetty or whatever for yourself, you don't need to use any special launcher.