Skip to content

Instantly share code, notes, and snippets.

@kishida
Last active March 18, 2026 18:23
Show Gist options
  • Select an option

  • Save kishida/17eaa5ab405970331e27f50786303348 to your computer and use it in GitHub Desktop.

Select an option

Save kishida/17eaa5ab405970331e27f50786303348 to your computer and use it in GitHub Desktop.
Javaコードを実行する
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 stopButton = new JButton("停止");
stopButton.setEnabled(false);
var button = new JButton("起動");
button.addActionListener(_ -> {
taLog.setText("");
button.setEnabled(false);
stopButton.setEnabled(true);
Thread.ofPlatform().start(() -> {
launch(taSource.getText(), taLog, stopButton);
SwingUtilities.invokeLater(() -> {
button.setEnabled(true);
stopButton.setEnabled(false);
});
});
});
stopButton.addActionListener(_ -> stopProcess());
var paste = new JButton("貼り付け");
paste.addActionListener(_ -> pasteFromClipboard(taSource));
var clip = new JButton("ログコピー");
clip.addActionListener(_ -> copyToClipboard(taLog.getText()));
var p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p.add(paste);
p.add(button);
p.add(stopButton);
p.add(clip);
f.add(BorderLayout.SOUTH, p);
Runtime.getRuntime().addShutdownHook(Thread.ofPlatform().unstarted(() -> {
if (currentProcess != null && currentProcess.isAlive()) {
currentProcess.destroyForcibly();
}
}));
f.setVisible(true);
split.setDividerLocation(split.getHeight() * 2 / 3);
}
void pasteFromClipboard(JTextArea target) {
try {
var contents = Toolkit.getDefaultToolkit()
.getSystemClipboard()
.getContents(null);
if (contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String) contents.getTransferData(DataFlavor.stringFlavor);
target.setText(text);
}
} catch (Exception ex) {
System.out.println(ex);
}
}
void copyToClipboard(String log) {
StringSelection sel = new StringSelection(log);
Toolkit.getDefaultToolkit()
.getSystemClipboard()
.setContents(sel, null);
}
final String JAVA_COMMAND = "C:\\Users\\naoki\\java\\jdk\\jdk-25\\bin\\java.exe";
Process currentProcess = null;
void stopProcess() {
if (currentProcess != null && currentProcess.isAlive()) {
currentProcess.destroy();
}
}
void launch(String source, JTextArea logArea, JButton stopButton) {
Path tempFile = null;
Path tempDir = null;
try {
tempDir = Files.createTempDirectory("compileTmp");
tempFile = tempDir.resolve("main.java");
Files.writeString(tempFile, source, StandardOpenOption.CREATE);
currentProcess = new ProcessBuilder(JAVA_COMMAND, tempFile.toString())
.redirectErrorStream(true)
.start();
try (BufferedReader br =
new BufferedReader(new InputStreamReader(currentProcess.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() + File.separator, "");
SwingUtilities.invokeLater(() -> logArea.append(l + "\n"));
}
}
currentProcess.waitFor();
if (currentProcess.exitValue() == 0) {
SwingUtilities.invokeLater(() -> logArea.append("no error\n"));
}
} catch (InterruptedException ex) {
SwingUtilities.invokeLater(() -> logArea.append("プロセスが停止されました\n"));
} catch (Exception ex) {
System.out.println(ex);
} finally {
currentProcess = null;
if (tempFile != null) {
try { Files.deleteIfExists(tempFile); } catch (IOException _) {}
}
if (tempDir != null) {
try { Files.deleteIfExists(tempDir); } catch (IOException _) {}
}
}
}
@kishida

kishida commented Dec 27, 2025

Copy link
Copy Markdown
Author
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment