Last active
December 28, 2025 03:07
-
-
Save kishida/17eaa5ab405970331e27f50786303348 to your computer and use it in GitHub Desktop.
Javaコードを実行する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import module java.desktop; | |
| void main() { | |
| var f = new JFrame("Javaランチャー"); | |
| f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| f.setSize(800, 600); | |
| f.setLocationRelativeTo(null); | |
| var split = new JSplitPane(JSplitPane.VERTICAL_SPLIT); | |
| var taSource = new JTextArea(); | |
| taSource.setLineWrap(false); | |
| taSource.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); | |
| split.add(JSplitPane.TOP, new JScrollPane(taSource, | |
| JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); | |
| var taLog = new JTextArea(); | |
| taSource.setLineWrap(true); | |
| split.add(JSplitPane.BOTTOM, new JScrollPane(taLog, | |
| JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); | |
| f.add(split); | |
| var button = new JButton("起動"); | |
| button.addActionListener(_ -> { | |
| taLog.setText(""); | |
| Thread.ofPlatform().start(() -> launch(taSource.getText(), taLog)); | |
| }); | |
| var clip = new JButton("ログコピー"); | |
| clip.addActionListener(_ -> copyToClipboard(taLog.getText())); | |
| var p = new JPanel(new FlowLayout(FlowLayout.RIGHT)); | |
| p.add(button); | |
| p.add(clip); | |
| f.add(BorderLayout.SOUTH, p); | |
| f.setVisible(true); | |
| split.setDividerLocation(split.getHeight() * 2 / 3); | |
| } | |
| void copyToClipboard(String log) { | |
| StringSelection sel = new StringSelection(log); | |
| Toolkit.getDefaultToolkit() | |
| .getSystemClipboard() | |
| .setContents(sel, null); | |
| } | |
| final String JAVA_COMMAND = "path-to-jdk/bin/java.exe"; | |
| void launch(String source, JTextArea logArea) { | |
| Path tempFile = null; | |
| Path tempDIr = null; | |
| try { | |
| // "main.java"というファイル名でテンポラリディレクトリに一時保存 | |
| tempDIr = Files.createTempDirectory("compileTmp"); | |
| tempFile = tempDIr.resolve("main.java"); | |
| Files.writeString(tempFile, source, StandardOpenOption.CREATE); | |
| // コンパイルしてエラーメッセージを表示 | |
| Process process = new ProcessBuilder(JAVA_COMMAND, tempFile.toString()) | |
| .redirectErrorStream(true) | |
| .start(); | |
| try (BufferedReader br = | |
| new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) { | |
| String line; | |
| boolean first = true; | |
| while ((line = br.readLine()) != null) { | |
| if (first && line.startsWith("Picked up")) { first = false; continue;} | |
| //一時フォルダ名部分を削除 | |
| String l = line.replace(tempDIr.toString() + "\\", "");; | |
| SwingUtilities.invokeLater(() -> logArea.append(l + "\n")); | |
| } | |
| } | |
| process.waitFor(); | |
| if (process.exitValue() == 0) { | |
| logArea.append("no error\n"); | |
| return; | |
| } | |
| } catch (Exception ex) { | |
| System.out.println(ex); | |
| } finally { | |
| // 一時ファイルを削除 | |
| if (tempFile != null) { | |
| try { | |
| Files.deleteIfExists(tempFile); | |
| } catch (IOException _) { | |
| } | |
| } | |
| // 一時ディレクトリを削除 | |
| if (tempDIr != null) { | |
| try { | |
| Files.deleteIfExists(tempDIr); | |
| } catch (IOException _) { | |
| // ignore cleanup failure | |
| } | |
| } | |
| } | |
| } | |
Author
kishida
commented
Dec 27, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment