Home > database >  Run java main class in command line/batch file/python script
Run java main class in command line/batch file/python script

Time:11-29

I have a java class which is to be made into a tool so that I can give the input to the class as parameters and then produce the output in the command line, I have written the code in IntelliJ and want the code to be portable so that it can be used instantly by the click of a button. I have little experience in creating a bat file or python script.

package ase.ATool;
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class AnaliT {
    public static void main(String[] args) {
        File file = null;
        File dir = new File(args[0]);
        File[] directoryListing = dir.listFiles();

        StringBuffer componentString = new StringBuffer(100);
        if (directoryListing != null) {
            for (File child : directoryListing) {
                float complexity = 0, noOfMethods = 0;
                System.out.println(child.getPath()   " ");
                String currentLine;
                BufferedReader bufferedReader = null;
                try {
                    bufferedReader = new BufferedReader(new FileReader(child.getPath()));
                    System.out.println(bufferedReader);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
}

I want to convert the above code in to a tool so i can invoke this main function from the command line, batch file or python file.

CodePudding user response:

You could compile and run the java file from python by checking out the subprocess module: https://docs.python.org/3/library/subprocess.html

You can also run java code using the system module: running a java file in python using os.system()

If you want to invoke Java code from python, take a look here: Calling Java from Python Here it is recommended you use Py4J.

Here you can find an example of running a Java Class from a .bat file: How to run java application by .bat file

As mentioned in a comment you can also compile the file with java using java target.java and then run it with java target in the same directory

I hope by showing you some of these resources you can guide yourself towards more specific help.

  • Related