1

I am very new on making Gui using java and its supporting libraries/classes.But I made calculator.I use to code in c++.So I found it difficult to give system call in java code to run c++ file.This is calc code implemented on eclipse editor using some libraries/classes.

    package calc;

    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;

    import java.awt.Font;

    public class calc_app {

        private JFrame frame;
        private JTextField textField;
        private JTextField textField_1;
        private JTextField textField_2;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        calc_app window = new calc_app();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the application.
         */
        public calc_app() {
            initialize();
        }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame();
            frame.setBounds(100, 100, 455, 297);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);

            textField = new JTextField();
            textField.setBounds(50, 52, 86, 20);
            frame.getContentPane().add(textField);
            textField.setColumns(10);

            textField_1 = new JTextField();
            textField_1.setBounds(226, 52, 86, 20);
            frame.getContentPane().add(textField_1);
            textField_1.setColumns(10);

            JButton btnNewButton = new JButton("ADD");
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        int num1,num2,ans;
                        num1=Integer.parseInt(textField.getText());
                        num2=Integer.parseInt(textField_1.getText());
                        ans = num1+num2;
                        textField_2.setText(Integer.toString(ans));
                    }catch(Exception arg2){

                        JOptionPane.showMessageDialog(null, "Please Enter valid Number");
                    }
                }
            });
            btnNewButton.setBounds(50, 105, 89, 23);
            frame.getContentPane().add(btnNewButton);

            JButton btnNewButton_1 = new JButton("SUBtract");
            btnNewButton_1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        int num1,num2,ans;
                        num1=Integer.parseInt(textField.getText());
                        num2=Integer.parseInt(textField_1.getText());
                        ans = num1-num2;
                        textField_2.setText(Integer.toString(ans));
                    }catch(Exception arg1){

                        JOptionPane.showMessageDialog(null, "Please Enter valid Number");
                    }
                }
            });
            btnNewButton_1.setBounds(223, 105, 89, 23);
            frame.getContentPane().add(btnNewButton_1);

            JLabel lblNewLabel = new JLabel("The Answer is ");
            lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 15));
            lblNewLabel.setBounds(50, 152, 142, 35);
            frame.getContentPane().add(lblNewLabel);

            textField_2 = new JTextField();
            textField_2.setBounds(226, 159, 86, 20);
            frame.getContentPane().add(textField_2);
            textField_2.setColumns(10);
        }

    }

My gui interface is following. enter image description here

I just want to run the one c++ file(say add.cpp ) when I click ADD Button. I also searched on google and also got many solutions but got very lengthy solution deviated from my case.

Bam
  • 141
  • 11

1 Answers1

2

You don't "run" C++ code or .cpp files; these are compiled into machine code and executed directly by the computer processor. I think it might be more helpful to you to try to edit your question to explain exactly why it is you think you want to run the .cpp file from your Java program. For this specific example, simply rewriting the calculations in Java would make the most sense.

To actually try to answer this question as-is, I think there's basically three options available to you, which I'll present in order from least insane to most insane.

  1. Compile the C++ code as a DLL, and then call the functions in the DLL from Java. The answers to this question here on SO might help with this route: How to call external dll function from java code
  2. Compile the C++ code as an executable that accepts command-line parameters, and then call that program from Java. e.g. create a program "add.exe" that can be called with "add.exe 5 10" and it'll print out "15". Then your Java program runs it with the appropriate parameters, and captures the output to display it to the user. You could also use files to pass parameters and results between the programs, or sockets, or any other method of inter-process communication. You could have a look at this question for ideas on how to run an external program and capture its output: Capture the output of an external program in JAVA
  3. Package a C++ compiler and all required include files and additional libraries needed to compile your helper programs. Have the Java program automatically compile the C++ source code into a program, and then do #2. This would allow you to have the source code with the program and for changes to that source code to actually be used. However, it also requires the entire toolchain necesary to compile and link the program to be present.
Community
  • 1
  • 1
notmyfriend
  • 245
  • 1
  • 7