java applet在部署时不运行,但在eclipse中运行时可以运行

时间:2012-12-18 17:19:50

标签: java eclipse applet

下面是我非常粗略的applet代码。在最底部,我执行client.clientProcess();

import javax.swing.*;

import com.pega.Messaging.Client;
import com.pega.Messaging.MessagingHelper;

import java.awt.event.*;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


@SuppressWarnings("serial")
public class PRPCPassCrackApplet extends JApplet{


    JLabel message;

    JLabel lpmsg;
    JTextField localPort;    

    JLabel snmsg;
    JTextField serverName;

    JLabel spmsg;
    JTextField serverPort;

    JButton button; 
    JLabel error;

    public void init(){

        setSize(600,150);  
        setBackground(Color.BLACK);

        message = new JLabel("Client Parameters\n");
        lpmsg = new JLabel("Local Port:  ");
        snmsg = new JLabel("Server Name: ");
        spmsg = new JLabel("Server Port: ");
        localPort = new JTextField();
        serverName = new JTextField();
        serverPort = new JTextField();
        localPort.setPreferredSize(new Dimension(450, 10));
        serverName.setPreferredSize(new Dimension(450, 10));
        serverPort.setPreferredSize(new Dimension(450, 10));

        button = new JButton("Send To Server");  
        button.setPreferredSize(new Dimension(450,10));

        error = new JLabel("");
        error.setForeground(Color.red);

        Container window = getContentPane();
        window.setLayout(new GridLayout(0,2)); 
        button.addActionListener(new ButtonListener());

        window.add(message);
        window.add(new JLabel(""));
        window.add(lpmsg);
        window.add(localPort);
        window.add(snmsg);
        window.add(serverName);
        window.add(spmsg);
        window.add(serverPort);
        window.add(button);
        window.add(error);
    }

    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent push){
            String lp = localPort.getText();
            String sn = serverName.getText();
            String sp = serverPort.getText();
            int lpint = -1;
            int spint = -1;
            ServerSocket listenSocket = null;
            Socket socketToServer = null;

            try{
                lpint = Integer.parseInt(lp);
            }catch(NumberFormatException e){
                error.setText("Local Port is not a number");
                return;
            }

            try{
                spint = Integer.parseInt(sp);
            }catch(NumberFormatException e){
                error.setText("Server Port is not a number");
                return;
            }

            try{
                listenSocket = new ServerSocket(lpint);
            }catch(Exception e){
                error.setText("Could not open local port: " + lp +". Choose another.");
                return;
            } finally{
                try {
                    listenSocket.close();
                } catch (IOException e) {
                }
            }

            try{
                //open connection to server
                InetAddress serverAddress = InetAddress.getByName(sn);
                socketToServer = new Socket(serverAddress, spint);
                //create initial connection 
                OutputStream os = socketToServer.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);

                //report in
                String status = "ping";
                bw.write(MessagingHelper.createPacket(status, sn, lp, ""));
                bw.close();

            }catch(Exception e){
                error.setText("Can't connect to Server: " + sn + " on port: " + sp);
                return;
            }finally{
                try {
                    socketToServer.close();
                } catch (IOException e) {
                }
            }
            error.setText("Beginning Execution");
            error.setForeground(Color.BLUE);
            button.setVisible(false);
            Client client = new Client(spint, sn, lpint, true);
            client.clientProcess();     

        }
    }
}

当我在eclipse中运行时,我的程序运行愉快并且正确完成,但是我通过右键单击>导出jar。出口> JAR文件我无法使用以下代码运行我的applet:

<!DOCTYPE html>

<html>

<head>
<title>Client Testing</title>
</head>

<body>
<h1 id="header1"> Distributed Brute Force Attacks </h1>


<applet
class="blah.blah.blah.blah.Applet" <!--not actually blah.blah I had to obfuscate-->
archive="client.jar"
code="blah.blah.blah.blah.Applet.class"
width="1000"
height="250"
vspace="10"
hspace="10"
align="center"
alt="You need Java to run this Applet"
name="clientApplet"
mayscript >
</applet>


</body>

</html>

如果它有助于eclipse我选中了“导出生成的类文件和资源”和“导出Java源文件和资源”,并且没有定义Main类。

applet正确显示并正确执行错误检查,但似乎永远不会运行该clientProcess。我该怎么做才能让用户通过applet运行我的代码?

1 个答案:

答案 0 :(得分:1)

确保设置了类路径。当您在eclipse中运行此applet时,所有类文件都存在于类路径中,包括jar。