[英]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.