gpt4 book ai didi

java - 在Java Applet中执行cmd命令

转载 作者:行者123 更新时间:2023-11-29 09:10:10 25 4
gpt4 key购买 nike

我正在尝试在 Java 小程序中执行 cmd 命令,我试过这段代码

import java.io.InputStream;
import java.io.IOException;
import java.applet.Applet;
import java.awt.*;
public class execute extends Applet{
String output="";
public void init(){
try {
// Execute command
String command = "MYCMDCOMMAND";
Process child = Runtime.getRuntime().exec(command);
// Get the input stream and read from it
InputStream in = child.getInputStream();
int c= in.read();
while ((c = in.read()) != -1) {
output =output+((char)c);
}
in.close();
}
catch (IOException e) {
}
System.out.println(output);
}
public void paint(Graphics g){
g.drawString(output,60,100);
}
}

然后写了这个html文件,保存在同一个目录下:

<html>
<head><title>Applet</title>
<body>
<applet code="execute.class",height="200" width="200">
</body>
</html>

我在这里要做的是在小程序中运行 ls shell 命令并显示结果。

代码编译没有错误。但是当我在浏览器中打开 html 文件时,我只看到一个灰色方 block 。

这是因为安全问题,我没有得到任何东西吗?还是因为代码错误?

最佳答案

有些事情你必须小心。

  1. 编译后必须将 Html 文件复制并粘贴到 .bin 文件中
  2. 你的 HTML 文件名是 Applet,你的类名是 execute.class,所以这意味着你没有包,如果你有你必须将你的 html 文件保存为“PACKAGENAME/execute.class”,
  3. 您必须在控制面板/Java 中禁用安全性

关于java - 在Java Applet中执行cmd命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12926213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com