栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Java程序中编译和运行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Java程序中编译和运行

您的代码可能失败的地方有很多,例如,由于某种原因它可能无法编译;

java
可能不在操作系统的搜索路径中;该
java
命令可能会失败,因为
src/HelloWorld
参数看起来不正确。

我相信您的命令应该看起来更像

java -cp ./src HelloWorld
或者应该在
src
目录的上下文中执行…

您确实需要尽一切努力来诊断潜在的问题,例如

DiagnosticCollector
针对
JavaCompiler
和阅读流程
InputStream
以及
ErrorStream

根据您

HelloWorld.java
和之前的答案,我能够生成此示例,该示例可以成功编译并运行您的类…

import java.io.File;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;import javax.tools.Diagnostic;import javax.tools.DiagnosticCollector;import javax.tools.JavaCompiler;import javax.tools.JavaFileObject;import javax.tools.StandardJavaFileManager;import javax.tools.ToolProvider;public class TestCompile {    public static void main(String[] args) {        StringBuilder sb = new StringBuilder(64);        try { DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {     // >>>> Compiler Stage     // This sets up the class path that the compiler will use.     // I've added the .jar file that contains the DoStuff interface within in it...     List<String> optionList = new ArrayList<>();     optionList.add("-classpath");     optionList.add(System.getProperty("java.class.path"));     Iterable<? extends JavaFileObject> compilationUnit          = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(new File("src/HelloWorld.java")));     JavaCompiler.CompilationTask task = compiler.getTask(          null,          fileManager,          diagnostics,          optionList,          null,          compilationUnit);     if (task.call()) {         // <<<< Compiler Stage         // >>>> Run stage...         ProcessBuilder pb = new ProcessBuilder("java", "-cp", "./src", "HelloWorld");         pb.redirectError();         Process p = pb.start();         InputStreamConsumer.consume(p.getInputStream());         p.waitFor();         // <<<< Run Stage     } else {         for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {  System.out.format("Error on line %d in %s%n",       diagnostic.getLineNumber(),       diagnostic.getSource().toUri());         }     } } catch (InterruptedException ex) {     Logger.getLogger(TestCompile.class.getName()).log(Level.SEVERE, null, ex); }        } catch (IOException exp) { exp.printStackTrace();        }    }    public static class InputStreamConsumer implements Runnable {        private InputStream is;        public InputStreamConsumer(InputStream is) { this.is = is;        }        public InputStream getInputStream() { return is;        }        public static void consume(InputStream is) { InputStreamConsumer consumer = new InputStreamConsumer(is); Thread t = new Thread(consumer); t.start();        }        @Override        public void run() { InputStream is = getInputStream(); int in = -1; try {     while ((in = is.read()) != -1) {         System.out.print((char)in);     } } catch (IOException exp) {     exp.printStackTrace(); }        }    }}

作为使用的替代方法

ProcessBuilder pb = new ProcessBuilder("java", "-cp", "./src","HelloWorld");
,您实际上可以使用…

ProcessBuilder pb = new ProcessBuilder("java", "HelloWorld");pb.directory(new File("src"));pb.redirectError();Process p = pb.start();

这将

java
src
目录的上下文中启动该过程



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507757.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号