0

So my problem here is that I'm not too sure how to print a set of commands to cmd. I have a batch file that runs a Minecraft server, and I need to be able to run commands through the command prompt that shows up when I run the batch file, which will in turn perform commands to the server.

Here is my code so far:

package com.Kaelinator;

import java.io.IOException;

public class ServerManager {
    public static void main(String[] args){

        try {
            System.out.println("Opening");
            Runtime runTime = Runtime.getRuntime();
            Process process = runTime.exec("cmd /C start /min " + "C:/Users/Owner/Desktop/rekt/Run.bat");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //System.out.println("Closing");
            //process.destroy();
        } catch (IOException e){
            e.printStackTrace();
        }

    }
}

Yes, I understand that all this does so far is open the batch file. :P I am expecting something like this that I need to add to my code:

process.InputStream(command1);

But I am certain that there is more to it, something along the lines of bufferedWriters or something like that...

Whenever I try to get answers from Google, the answers always have a whole load of extra code, or have something completely different about them. Thanks!

Jonny Henly
  • 3,725
  • 4
  • 23
  • 41
Kaelinator
  • 350
  • 2
  • 15
  • are you asking how to start another program or how to write to stdin on the program you're calling – Natecat Apr 11 '16 at 22:31
  • @Natecat the above code runs a batch file, I just want to know how to print to that same batch file – Kaelinator Apr 11 '16 at 22:38
  • You want to modify the batch file that you are running? – Natecat Apr 11 '16 at 22:41
  • If you want to write to the file, you probably looking for [that](http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java) – ibm701 Apr 11 '16 at 22:43
  • 1
    Possible duplicate of [Writing to InputStream of a Java Process](http://stackoverflow.com/questions/18903549/writing-to-inputstream-of-a-java-process) – Natecat Apr 11 '16 at 22:51
  • Not exactly what I meant, but that will defiantly be helpful as a I go farther into this project! @ibm701 – Kaelinator Apr 11 '16 at 22:56
  • @Natecat I'll try to comprehend that xD – Kaelinator Apr 12 '16 at 00:19
  • @Natecat ok... I tried adding this `OutputStream stdin = (OutputStream) process.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin)); writer.write("say HI");` and I get this error after the Thread.sleep: `Exception in thread "main" java.lang.ClassCastException: java.io.BufferedOutputStream cannot be cast to org.omg.CORBA.portable.OutputStream at com.Kaelinator.ServerManager.main(ServerManager.java:21) ` what did I do wrong? – Kaelinator Apr 12 '16 at 00:50
  • Why are you casting anything? process,getOutputStream() already returns an OutputStream – Natecat Apr 12 '16 at 00:55
  • @Natecat Sorry, I was using some kind of COBRA import instead of the java.io imports. I changed them back to the java.io imports and I took out the cast, now it doesn't seem to do anything at all. – Kaelinator Apr 12 '16 at 01:03
  • @Natecat Not too sure if an OutputStream is the way to go with the goal I'm trying to accomplish. From the examples I've seen online, they seem to write a text file, while I'm trying to send commands to a running command prompt... any ideas? – Kaelinator Apr 12 '16 at 01:07
  • Did you read the question I linked and the solution? – Natecat Apr 12 '16 at 01:08
  • @Natecat I did, but since this is something completely different than anything I've ever done before, It's very confusing... I'm still trying – Kaelinator Apr 12 '16 at 01:15
  • I'm talking about the actual problem. He is having exactly the same issue as you are now, read the question again. – Natecat Apr 12 '16 at 01:18
  • Yeah I'll just ask my computer science teacher about it. xD That's a tad too complicated for me to understand without someone explaining it to me, thank you though! – Kaelinator Apr 12 '16 at 01:30

0 Answers0