2

I want to run and pass the arguments to ".cmd" file using Java program. I have checked the existing Solutions, but nothing is working in my case.

From Command line, I am running below command after getting into Directory C:/users/project/solr/bin

solr.cmd -s "C:users/github/example/solr-config"

So, solr.cmd gets arguments from the other Directory and then it runs the solr instance.

I have tried this, but I am not sure how to provide Parameters to Runtime.getRuntime():

     Runtime run = Runtime.getRuntime();

     Process p = null;  

     String cmd = "cmd /c start C:/users/project/solr/bin C:users/github/example/solr-config";
     Process pr = run.exec(cmd);

I have followed this link: How do I run a batch file from my Java Application?

Could anyone please help me this.

Rajjat Dadwal
  • 183
  • 1
  • 18
  • 1
    Possibly a duplicate of https://stackoverflow.com/questions/615948/how-do-i-run-a-batch-file-from-my-java-application – Piotr Wilkin Apr 27 '18 at 10:41
  • 1
    This is a super common problem. If you have not-working code, show a [mcve]. If you have read other questions, link to them in your question. – GhostCat Apr 27 '18 at 10:41
  • @GhostCat I have add what i have done so far. – Rajjat Dadwal Apr 27 '18 at 10:51
  • Please read [mcve] again. "code not working" is not acceptable input here. – GhostCat Apr 27 '18 at 10:52
  • 1
    Your command xdoes not call `solr.cmd`. What you want is `cmd /c start C:/users/project/solr/bin/solr.cmd C:/users/github/example/solr-config` – Piotr Wilkin Apr 27 '18 at 10:53
  • @PiotrWilkin I have tried that also, it does not Show anything. Since from terminal we also specify ```-s``` after ```solr.cmd```. Do you think, we have to specify that also? – Rajjat Dadwal Apr 27 '18 at 10:56

2 Answers2

1

I solved it by modifying cmd as solr.cmd expects -s also as argument:

String cmd = "cmd /c start C:/users/project/solr/bin/solr.cmd -s C:users/github/example/solr-config;
Rajjat Dadwal
  • 183
  • 1
  • 18
0

There is a difference between "it doesn't work" and "it doesn't show anything". As per the documentation:

By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

If you want to retrieve the data that your process might have printed out on the standard I/O, you will have to read them from the abovementioned streams.

Compo
  • 36,585
  • 5
  • 27
  • 39
Piotr Wilkin
  • 3,446
  • 10
  • 18