rik
rik

Reputation: 1299

Reading jar stream in c# from the console

i found some code online to run my jar (which outputs to the console either true or false) from c# and read it in. On my development machine the following code works fine

    private bool hasInformation(String value)
    {
        try
        {

            using (Process p = new Process())
            {
                p.StartInfo = new ProcessStartInfo("java", @" -jar myJarName.jar " + value);
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                String availableOrNot = p.StandardOutput.ReadToEnd();

                p.WaitForExit();
                //Trace.WriteLine("data = " + s);

                availableOrNot = availableOrNot.Trim();
                if (availableOrNot.Equals("true"))
                {
                    return true;
                }
                return false;
            }
        }
        catch (Exception e)
        {
            LogWriter.writeToLogFile(e);
        }
        return false;
    }

The problem is that on my testing machine the code returns just an empty string as if its throwing an exception before it can execute.

I have no idea what else to check for as the jar file runs fine in the command line window when i manually do it and no exception is being thrown (well its not being caught at least) . All that happens is that i get an empty string, where as on my development machine i get a true or false answer.

Hopefully someone might have some ideas of what might be going wrong so that i can investigate as i am really stuck.

Thanks

Edit : After redirecting the error message so i can read that from the stream i get the error

Unable to access jarfile myJarName.jar\r\n"

Upvotes: 2

Views: 2054

Answers (3)

Daniel B
Daniel B

Reputation: 2887

Based on the new info you posted, it's probably either the working directory or the CLASSPATH environment variable that's wrong.

Check the working directory to see if it's what you expect (I'm guessing it should be the same as that JAR file's directory), or set it:

p.StartInfo.WorkingDirectory = "C:\\path to my JAR file";

Alternatively, set the CLASSPATH:

p.StartInfo.EnvironmentVariables["CLASSPATH"] = "C:\\.....";

Upvotes: 2

Nicola Musatti
Nicola Musatti

Reputation: 18226

The most likely cause of your problem appears to be that your CLASSPATH environment variable is set differently on the two machines. In one case it includes myJarName.jar and in the other it doesn't.

Upvotes: 1

Alexander Beletsky
Alexander Beletsky

Reputation: 19831

You have to correct your code, to allow read the output. You have to initialize RedirectStandardOutput. Example,

        var processInfo = new ProcessStartInfo(commandName, commandArgumentsForLogging);
        processInfo.CreateNoWindow = true;
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;
        processInfo.UseShellExecute = false;
        processInfo.ErrorDialog = false;

        var p = new Process { StartInfo = processInfo };

Upvotes: 0

Related Questions