[英]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它可以通过单击按钮立即使用。 我在创建 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();
}
}
我想将上面的代码转换成一个工具,这样我就可以从命令行、批处理文件或 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
您还可以使用系统模块运行 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 .
在这里您可以找到从 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
我希望通过向您展示其中一些资源,您可以引导自己获得更具体的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.