简体   繁体   English

在命令行/批处理文件/python脚本中运行java main class

[英]Run java main class in command line/batch file/python script

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 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它可以通过单击按钮立即使用。 I have little experience in creating a bat file or python script.我在创建 bat 文件或 python 脚本方面几乎没有经验。

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.我想将上面的代码转换成一个工具,这样我就可以从命令行、批处理文件或 python 文件调用这个主 function。

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 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()您还可以使用系统模块运行 java 代码: 使用 os.system() 在 python 中运行 java 文件

If you want to invoke Java code from python, take a look here: Calling Java from Python Here it is recommended you use Py4J . 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在这里您可以找到从 a.bat 文件运行 Java Class 的示例: 如何通过.bat 文件运行 java 应用程序

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 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.我希望通过向您展示其中一些资源,您可以引导自己获得更具体的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中运行包含 java 命令的批处理文件 - Run a batch file in Python which contains java command 如何运行转换此批处理脚本,使其可以在命令行上运行并被 python 调用? - How to run convert this batch script so it can run on command line and be called by python? 命令行python脚本在不同目录中的文件上运行 - command line python script run on a file in different directory 通过命令行在目录中的每个文件中运行 python 脚本 - run python script in every file in a directory through command line 如何在我的 python 脚本中运行批处理命令? - How to run batch command in my python script? 无法使用Windows命令行和批处理文件运行脚本 - Can't get script to run using windows command line with batch file HTMLHelp编译器可从命令行完美运行,但不能从脚本或批处理文件运行 - HTMLHelp compiler works perfectly from command line but not run from a script or batch file 每当我尝试在命令行中运行python脚本时,“无法在'__main__'模块中找到”错误 - “Can't find '__main__' module in” error whenever I try to run a python script in the command line 在Windows批处理文件中运行Python命令 - Run Python command in a Windows batch file 命令python:命令行和批处理脚本中的不同含义 - Command python: different meanings in command line and batch script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM