none
How can i interact with a java commandline application?

    Question

  • Hello

    I have this server for a game called Minecraft, and i want to try and create an automatic announcer, but i don't have any idea how i can make my application write in the java server, which is commandline based.

    Any help? would be much appriciated
    Wednesday, June 24, 2009 10:30 PM

Answers

  • OK I tried this and it'll open the window but it immediately goes not responding, don't really have time to keep goin on it, but here's a starting point. And I just added CreateNoWindow so it no longer goes not responding, but I also can't tell if it works. So good luck!

                System.Diagnostics.Process myProcess = new System.Diagnostics.Process
                {
                    StartInfo = new System.Diagnostics.ProcessStartInfo()
                    {
                        FileName = @"C:\myjavaexe.exe",
                        UseShellExecute = false,
                        RedirectStandardInput = true,
                        CreateNoWindow = true
                    }
                };
    
                myProcess.Start();
                System.IO.TextWriter textWriter = myProcess.StandardInput;
    
                textWriter.WriteLine("This goes to the process.");
    • Marked as answer by Harry Zhu Wednesday, July 01, 2009 8:45 AM
    Thursday, June 25, 2009 7:43 PM

All replies

  • Which language is your application is written? if your program is written in Java, then you are in wrong forum, there is java Sharp (J#) forum in this MSDN but it almost dead because microsoft was discontinued the product from its VS environment and this lead to fewer developers that use the language. This is the J# forum http://social.msdn.microsoft.com/Forums/en-US/visualjsharpgeneral/threads, you can try place your question there but no guarantee that it will be answered.  You may google or bing for Java forum.
    On the other hand, if your program is C# or VB and try to communicate with java server, then wait maybe someone will have solution for you

    kaymaf


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Thursday, June 25, 2009 1:03 AM
  • Which language is your application is written? if your program is written in Java, then you are in wrong forum, there is java Sharp (J#) forum in this MSDN but it almost dead because microsoft was discontinued the product from its VS environment and this lead to fewer developers that use the language. This is the J# forum http://social.msdn.microsoft.com/Forums/en-US/visualjsharpgeneral/threads , you can try place your question there but no guarantee that it will be answered.  You may google or bing for Java forum.
    On the other hand, if your program is C# or VB and try to communicate with java server, then wait maybe someone will have solution for you

    kaymaf


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Nope, it's written in c#.

    Thanks for the reply tho :3

    I'll just wait and see if anyone replies.
    Thursday, June 25, 2009 8:06 AM
  • Can you communicate with the server in a console application?
    Thursday, June 25, 2009 2:00 PM
  • Yes i can

    It's as simple as typing in the chat, "/say text" sends a text announcement onto the server
    Thursday, June 25, 2009 6:47 PM
  • Yes i can

    It's as simple as typing in the chat, "/say text" sends a text announcement onto the server

    I don't understand.  Post your console application code and explain what modifications you want.
    Thursday, June 25, 2009 7:01 PM
  • I think there's an existing java console application and he wants to make a program that will send it text commands.

    Thursday, June 25, 2009 7:09 PM
  • I think there's an existing java console application and he wants to make a program that will send it text commands.

    Yes.

    I don't have any code yet, because i want to know if it's possible before i start.

    I'll try and explain with detail:

    Imagine you running command prompt, then with another application, you want to control the command promt, by typing in text in your application and press "start" or "send" etc. then the things you put in your application, will run in the command promt, those exact text lines, etc.

    Sorry if i'm confusing, i'm not a programming expert :3
    Thursday, June 25, 2009 7:12 PM
  •             Process myProcess = new Process()
                {
                    StartInfo.FileName = @"filepathtoexe";
                };
    
                myProcess.Start();
                TextWriter textWriter = process.StandardInput;
    
                textWriter.WriteLine("This goes to the process.");
    Never tried this, maybe it'll work.
    Thursday, June 25, 2009 7:19 PM
  • Ok.  I'm running a command prompt.  Now how does the other application control my command prompt?  How did the other application get started?
    Thursday, June 25, 2009 7:29 PM
  • OK I tried this and it'll open the window but it immediately goes not responding, don't really have time to keep goin on it, but here's a starting point. And I just added CreateNoWindow so it no longer goes not responding, but I also can't tell if it works. So good luck!

                System.Diagnostics.Process myProcess = new System.Diagnostics.Process
                {
                    StartInfo = new System.Diagnostics.ProcessStartInfo()
                    {
                        FileName = @"C:\myjavaexe.exe",
                        UseShellExecute = false,
                        RedirectStandardInput = true,
                        CreateNoWindow = true
                    }
                };
    
                myProcess.Start();
                System.IO.TextWriter textWriter = myProcess.StandardInput;
    
                textWriter.WriteLine("This goes to the process.");
    • Marked as answer by Harry Zhu Wednesday, July 01, 2009 8:45 AM
    Thursday, June 25, 2009 7:43 PM
  • Ok, so i got the program running fine with the code you wrote Scotty,

    static void Main(string[] args)
            {
                System.Diagnostics.Process myProcess = new System.Diagnostics.Process
                {
                    StartInfo = new System.Diagnostics.ProcessStartInfo()
                    {
                        FileName = @"start server.bat",
                        UseShellExecute = false,
                        RedirectStandardInput = true,
                        Arguments = @"java -cp minecraft-server.jar com.mojang.minecraft.server.MinecraftServer",
                    }
                };
    
                myProcess.Start();
                System.IO.TextWriter textWriter = myProcess.StandardInput;
    
                textWriter.WriteLine("/say test");
            }


    The only thing that doesn't work is the writeline, i also get this in the server log as i start the server:

    stdin: end of file! No more direct console input is possible.

    Edit:

    I got it working, i put Console.Writeline instead.

    Don't know if this is a good way to do it, but if it is, i'm sure someone will tell me.
    • Edited by Coolman12 Thursday, June 25, 2009 8:27 PM
    Thursday, June 25, 2009 8:00 PM
  • Can't you start MinecraftServer dircectly?  If not, post the server.bat code.
    Thursday, June 25, 2009 8:20 PM
  • Does that work? Sending arguments to a batch file? Shouldn't those arguments be IN the batch file? And depending how long it takes to start up you might need to throw a Thread.Sleep(x); in there.
    Thursday, June 25, 2009 8:26 PM
  • Does that work? Sending arguments to a batch file? Shouldn't those arguments be IN the batch file? And depending how long it takes to start up you might need to throw a Thread.Sleep(x); in there.

    I'm glad you know exactly what he's trying to do.  How do you know how he normally starts the server?  Does he start the .bat file from a command prompt.  Does the .bat file take arguments?  I'm playing 20 questions and you're playing Jeopardy with multiple answers.
    Thursday, June 25, 2009 8:34 PM
  • I don't use the arguments anymore, sorry, forgot to edit it out.

    Code:
    System.Diagnostics.Process myProcess = new System.Diagnostics.Process
                {
                    StartInfo = new System.Diagnostics.ProcessStartInfo()
                    {
                        FileName = "start server.bat",
                        UseShellExecute = false,
                        RedirectStandardInput = true,
                    }
                };
    
                myProcess.Start();
                System.IO.TextWriter textWriter = myProcess.StandardInput;
    
    That seems to work.

    i also know how i can interact with the server now(doesn't show in code), so i guess the question is answered.

    I wont mark answer yet tho, i'll wait and see if any more errors show up.
    Thursday, June 25, 2009 9:09 PM
  • If you have another problem you should open a new topic, and reference this one if you want. This way the topic will be accurate and it helps people later when they find the threads if they have one question, one answer.
    Friday, June 26, 2009 1:15 PM