简体   繁体   English

在Java Applet中执行cmd命令

[英]Execute cmd command in Java Applet

I'm trying to execute cmd command in Java applet, I tried this code 我正在尝试在Java applet中执行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);
}
}

And then wrote this html file and saved it in the same directory: 然后编写此html文件并将其保存在同一目录中:

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

What I'm trying to do here is to run the ls shell command in an applet and display the results. 我在这里要做的是在applet中运行ls shell命令并显示结果。

The code compiles with no errors. 该代码编译没有错误。 But when I open the html file in the browser, I just get a gray square. 但是,当我在浏览器中打开html文件时,我只会看到一个灰色方块。

Is this because of security issues that I don't get anything? 是因为安全问题我什么也没得到吗? Or is it because of an error in the code? 还是因为代码错误?

You must be careful about some things. 您在某些事情上必须小心。

  1. You have to Copy & paste your Html file to .bin file after you compiled 编译后,您必须将HTML文件复制并粘贴到.bin文件中
  2. Your HTML file name is Applet and your class name execute.class so it means you do not have package if u have you must save your html file as "PACKAGENAME/execute.class", 您的HTML文件名为Applet,类名为execute.class,因此,如果您必须将html文件另存为“ PACKAGENAME / execute.class”,则表示您没有软件包,
  3. At Control Panel / Java you must disable Security 在控制面板/ Java中,您必须禁用安全性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM