0

I have a requirement to execute a .jar file in another directory. Following is the code I attempted to perform this task,

        try
        {

            Process p = new Process();
            p.StartInfo.FileName = @"java cp D:\nfc\nfctools-examples-M9\nfctools-examples-M9\src\main\java\nfctools-examples.jar";//, @"-cp nfctools-examples.jar org.nfctools.examples.hce.HceDemo");
            p.StartInfo.Arguments = @"org.nfctools.examples.hce.HceDemo";
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.Start();
            String s = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            textBox1.Text = textBox1.Text + "data = " + s;
        }
        catch (Exception ex) {
            MessageBox.Show(ex.ToString());
        }

But I get the error The system cannot find the file specified error. I did the task referring to the posts,

I would be really grateful if you experts could assist me in fixing this error. Thank you very much :)

Edit

the path org.nfctools.examples.hce.HceDemo is in D:\nfc\nfctools-examples-M9\nfctools-examples-M9\src\main\java\.

The folder stucture is,

D:\
    nfc\
        nfctools-examples-M9\
            nfctools-examples-M9\
                src\
                    main\
                        java\
                            *nfctools-examples.jar
                            |org\
                            |   nfctools\
                            |       examples\
                            |           hce\
                            |               HceDemo.java

The * represents the .jar file that needs to be executed and the pipe symbol represents the class path cp shown by org.nfctools.examples.hce.HceDemo.

This execution is done in command prompt via java -cp nfctools-examples.jar org.nfctools.examples.hce.HceDemo from within the D:\nfc\nfctools-examples-M9\nfctools-examples-M9\src\main\java\ folder.

My requirement is to perform the execution via C# and obtain the result of the jar file execution to a textbox.

Community
  • 1
  • 1
Hasitha Shan
  • 2,900
  • 6
  • 42
  • 83

1 Answers1

1

The file specified by FileName must exist. Don't add any arguments to this property. Therefore, you have to turn your code into this:

p.StartInfo.FileName = @"java";
p.StartInfo.Arguments = @"cp D:\nfc\nfctools-examples-M9\nfctools-examples-M9\src\main\java\nfctools-examples.jar org.nfctools.examples.hce.HceDemo";
Sayse
  • 42,633
  • 14
  • 77
  • 146
AcidJunkie
  • 1,878
  • 18
  • 21