java - 如何使用 tools.jar 编译方法打印 java 编译器错误日志?
问题描述
在我的idea IDE中,我可以在控制台看到红色字体的编译错误。但是当我在linux服务器中部署jar时,我看不到编译日志。如何打印编译错误日志?
public static void main(String[] args) throws Exception {
String compliePath="D:\\testFole";
String filename="D:\\test.java";
String[] arg = new String[] { "-d", compliePath, filename };
System.out.println(com.sun.tools.javac.Main.compile(arg));
}
解决方案
好吧,如果我的问题是正确的,那么这是一种结果方法。我认为这将与平台无关。
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
private static Process process;
public static void main(String[] args) {
runCommand();
getErrorMessage();
}
/**
* This method executes/runs the commands
*/
private static void runCommand()
{
File file = new File("D:\\\\test.java");
String changeDirectory = "cmd start cmd.exe /c cd D:\\";
String compile = " && javac D:\\test.java";
String run = " && java "+file.getName().replace(".java","");
String command = changeDirectory + compile + run;
try {
process = Runtime.getRuntime().exec(command);
}catch (IOException e){}
}
/**
* This method will get the errorStream from process
* and output it on the console.
*/
private static void getErrorMessage()
{
try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())))
{
String line;
if(errorReader.readLine() != null)
while ((line = errorReader.readLine()) != null)
System.out.println(line); //display error message
}catch (IOException e){}
}
}
推荐阅读
- amazon-web-services - 在 Visual Studio Team Explorer 中连接到另一个 AWS 账户
- groovy - 具有包含点 (.) 的变量名称的 Groovy 列表被转换为字符串
- c++ - 在 C++ 中销毁动态分配的内存(数组对象)
- python - auth_user.username 给出 'NoneType' 对象没有属性 'username'
- python-3.x - 频繁或批量使用 REST API 端点自动提取事件
- python - 查找图像是亮还是暗
- r - bind_rows_(x, .id) 中的错误:参数 1 必须有名称
- c# - 如何捕获 System.IO.FileSystemWatcher 抛出的异常
- swift - CMSensorRecorder - 读取历史数据有时返回 nil
- javascript - Showing month name from users input using JS switch case