0

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.

  • You can a compile the java file using "javac" and then run it with "java". Are you looking to do something else here ? – Kujoen Nov 28 '21 at 21:24
  • Or do you mean you want to create a python script to run this ? Please a little more information :) – Kujoen Nov 28 '21 at 21:29
  • yup a python script would be nice, so that a simple run of the script with arguments completes the work – ThatStudent Nov 28 '21 at 21:32
  • Python script to run Java? Import `os`, then in the same folder run something like `os.system("javac AnaliT.java")` then `os.system("java Anali")`. With the command line just go to the same folder and run `javac AnaliT.java` and then `java AnaliT`. – code Nov 28 '21 at 21:37

1 Answers1

0

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.

Kujoen
  • 173
  • 1
  • 8