Call a jar file in C++ program

You are here means, you are trying to call jar file of Java project from C++ program. You can easily achieve this using the C++ system function. You can also pass arguments to the Java class from the C++ program.

In this article, we are going to learn how to call a jar file in C++ using a system function. We will also study, how to pass arguments to the java class from the C++ program.

Create a java project and generate runnable jar:

Following is the simple java program that we will call from the c++ program. We have extracted our java project and created a runnable jar using eclipse. Following is the Project structure in eclipse.

call jar from c++ program

Call a jar file without arguments from C++ program:

In the following scenario, we are calling jar a file from the C++ program without passing any argument values to the java program.

To call jar file from the C++ program we have to use the system() function. System() is an operating level function.

Following is the required syntax of the command to execute a jar file.

Java –jar <path of the jar file>

C++ Program

#include<stdio.h>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    string jarFilePath ="";
    jarFilePath.assign("java -jar ");
    jarFilePath.append("C:\\Users\\Technolads\\Desktop\\code\\sample.jar");

    
    printf("Jar execution path:  %s",jarFilePath.c_str());
    
    system(jarFilePath.c_str());
    
}

Java program

In the following code, we have used JOpionPane class to display a simple message in the message box to the user when the code is executed.

package com.technolads;

import javax.sound.midi.SysexMessage;
import javax.swing.JOptionPane;

public class welcome {

	public static void main(String[] args) 
	{
	    // TODO Auto-generated method stub
	    
            JOptionPane.showMessageDialog(null, "Welcome to Technolads", "" 
    
            + "", JOptionPane.INFORMATION_MESSAGE);

	}

}

Call a jar file with arguments from C++ program:

In the following scenario, we are calling jar a file from the C++ program by passing argument values to the java program.

An argument passed to the Java class is received through main method of the java class. There is a parameter String[] args which receives and stores values that we are passing from the C++ program. Its array parameter means it can store multiple values.

C++ Program

You can also pass arguments to a jar file from C++ program.

Following is the syntax to pass arguments from the C++ program to the java class.

Java –jar <path of the jar file>  <argument1> <argument2>

Refer below sample C++ program to pass arguments to a jar file.

#include<stdio.h>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    string jarFilePath ="";
    jarFilePath.assign("java -jar ");
    jarFilePath.append("C:\\Users\\Technolads\\Desktop\\code\\sample.jar");
    jarFilePath.append(" Technolads");
    
    printf("Jar execution path:  %s",jarFilePath.c_str());
    
    system(jarFilePath.c_str());
    
}

Java program

All arguments passed to the java class is received by the String[] args array variable. You can pass multiple values to the Java class.

Suppose you pass two arguments to the java class then receive these two values as shown below.

String inputArgument1 = args[0];

String inputArgument2 = args[1];

package com.technolads;

import javax.sound.midi.SysexMessage;
import javax.swing.JOptionPane;

public class welcome {

	public static void main(String[] args) 
	{
		String inputArguments = args[0];
		
		String outputString = "Welcome to " + inputArguments;
		// TODO Auto-generated method stub
		JOptionPane.showMessageDialog(null, outputString, "" 
				+ "", JOptionPane.INFORMATION_MESSAGE);

	}

}

Output:

When the C++ program is executed, it will call jar file and the java program will display the following message on the screen.

call java program in c++

Also check how to use system(“pause”) function in c++.

What did you learn in this article?

To summarize, we have learned the following points in this article:

  1. Call a jar file in C++ program using system function.
  2. Pass arguments to the java class from C++ function.

Final thoughts:

We hope this article was informative and you are able to execute your jar file from the C++ program. If you are facing any issue then please do mention it in the comment section or you can reach out to us using the contact form.

Leave a Comment