[.net, Java integration] Nger that executes .jar in C # .net

This article is the same as this article on my site.

If you need to start a process from C # .net that you can't do without using Java It's hard to implement that ... Come on! Nger running .jar in C # .net !!

Java side program

The program on the java side puts the main method (function). Receives a value from C # with the command line argument "args" of the main function. In the sample program below, the message: msg is received and displayed with one argument.

Sample.java


	public static void main(String[] args) {
		//TODO auto-generated method stub
		if (args.length <= 0) {
			System.out.println("No output message");
		} else {
			System.out.println("Output message:" + args[0]);
		}
		//Waiting for Enter key input
		//Reference: https://stackoverflow.com/questions/26184409/java-console-prompt-for-enter-input-before-moving-on
		System.out.println("Press \"ENTER\" to exit...");
		Scanner scanner = new Scanner(System.in);
		scanner.nextLine();
	}

Once you have a program, let's generate an executable .jar file.

Program on the C # (.net) side

On the C # side, start the .jar file with an object of the Process class. Placing the .jar file in the Debug/Release folder where the C # (.net) executable file is generated makes it easier to specify the file.

//When displaying and executing the command prompt
Process.Start("java", "-jar (.jar filename or path) (argument) (argument)…"))
//When executing without displaying the command prompt
Process.Start("javaw", "-jar (.jar filename or path) (argument) (argument)…"))

If you want to stop the C # program while the .jar file is running, use the WaitForExit method.

//Waiting for the end
jar.WaitForExit();

The execution result of the .jar file can be received by the ExitCode method.

//Get results(0:Successful completion)
jar.ExitCode();

Since the object will be created, don't forget to release the object by using, close, dispose, etc. Reference: How to remember to release the reserved resources? [C # / VB] Implementation example ↓

ConnectJar.cs


        /// <summary>
        /// .jar execution
        /// </summary>
        /// <param name="msg">message</param>
        /// <returns>True:success/False:Failure</returns>
        public static bool Excecute(string msg)
        {
            bool result = false;
            Process jar = null;
            try
            {
                // .Start jar as a process
                using (jar = Process.Start("java", "-jar Sample.jar " + msg))
                {
                    //Waiting for the end
                    jar.WaitForExit();
                    //Get results(0:Successful completion)
                    if (jar.ExitCode == 0) result = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Exception occurred\n" + e.Message);
            }
            return result;
        }

All you have to do is run C # .net ...! The entire sample code can be found in the repository below. CSJar Source Code

Supplement

--This time, I implemented it in C #, but you can execute .jar in the same way on VB.net. --You can use the Process class to execute various processes other than .jar. Launch an external application, open the file with the associated software | dobon.net

Recommended Posts

[.net, Java integration] Nger that executes .jar in C # .net
Reproduce Java enum in C #
Java version notation that changes in Java 10
Read Java properties file in C #
Java, JavaScript, C # (difference in assignment)
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Java that outputs vmg format file in eml format
Differences in writing Java, C # and Javascript classes
A bat file that uses Java in windows
General-purpose StringConverter class that utilizes generics in Java8