[Java] probleem met compileren SSL server

Pagina: 1
Acties:

  • pjonk
  • Registratie: November 2000
  • Laatst online: 29-12-2025
Ik wil het een en ander gaan doen met SSL in Java. Ik ben vervolgens gaan zoeken op internet en heb de JSSE library gedownload
http://java.sun.com/products/jsse/index-102.html

Ik heb de manual doorgelezen en vervolgens ben ik een sample gaan uitproberen die bij de package zat.

De sample was een mini webserver met 2 .java files.
Vervolgens ben ik naar JBuilder gegaan en heb de JSSE library JAR files toegevoegd.

Vervolgens heb ik de 2 .java files toegevoegd aan mijn project (ClassFileServer.java en ClassServer.java) en alles compileerde goed.

Nu wil ik dat Java progsel starten via de commandline via:
Java ClassFileServer

Maar nu krijg ik elke keer de melding:
Exception in thread "main" java.lang.NoClassDefFoundError: ClassFileServer

Weet iemand waar dit aan kan liggen?
Alvast bedankt voor de hulp.

It’s nice to be important but it’s more important to be nice


  • roelio
  • Registratie: Februari 2001
  • Niet online

roelio

fruitig, en fris.

nu ja die melding lijkt mij duidelijk, er is geen class ClassFileServer gevonden. Heb je de .java hiervan wel gecompiled? Post anders eens wat code uit de main() ?

AMD Phenom II X4 // 8 GB DDR2 // SAMSUNG 830 SSD // 840 EVO SSD // Daar is Sinterklaas alweer!!


  • wasigh
  • Registratie: Januari 2001
  • Niet online

wasigh

wasigh.blogspot.com

meestal is dat gezeur met packages, check je classpath

  • pjonk
  • Registratie: November 2000
  • Laatst online: 29-12-2025
Ik heb de Java files zonder foutmeldingen gecompiled naar Class files.

Een aantal imports, importeren data uit 3 JAR files met die in de LIB directory staan.

Code ClassFileServer.java
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.io.*;
import java.net.*;
import java.security.KeyStore;
import javax.net.*;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;
import com.sun.net.ssl.*;

/* ClassFileServer.java -- a simple file server that can server
 * Http get request in both clear and secure channel
 *
 * The ClassFileServer implements a ClassServer that
 * reads files from the file system. See the
 * doc for the "Main" method for how to run this
 * server.
 */

public class ClassFileServer extends ClassServer {

    private String docroot;

    private static int DefaultServerPort = 2001;

    /**
     * Constructs a ClassFileServer.
     *
     * @param path the path where the server locates files
     */
    public ClassFileServer(ServerSocket ss, String docroot) throws IOException
    {
    super(ss);
    this.docroot = docroot;
    }

    /**
     * Returns an array of bytes containing the bytes for
     * the file represented by the argument <b>path</b>.
     *
     * @return the bytes for the file
     * @exception FileNotFoundException if the file corresponding
     * to <b>path</b> could not be loaded.
     */
    public byte[] getBytes(String path)
    throws IOException
    {
    System.out.println("reading: " + path);
    File f = new File(docroot + File.separator + path);
    int length = (int)(f.length());
    if (length == 0) {
        throw new IOException("File length is zero: " + path);
    } else {
        FileInputStream fin = new FileInputStream(f);
        DataInputStream in = new DataInputStream(fin);

        byte[] bytecodes = new byte[length];
        in.readFully(bytecodes);
        return bytecodes;
    }
    }

    /**
     * Main method to create the class server that reads
     * files. This takes two command line arguments, the
     * port on which the server accepts requests and the
     * root of the path. To start up the server: <br><br>
     *
     * <code>   java ClassFileServer <port> <path>
     * </code><br><br>
     *
     * <code>   new ClassFileServer(port, docroot);
     * </code>
     */
    public static void main(String args[])
    {
    System.out.println(
        "USAGE: java ClassFileServer port docroot [TLS [true]]");
    System.out.println("");
    System.out.println(
        "If the third argument is TLS, it will start as\n" +
        "a TLS/SSL file server, otherwise, it will be\n" +
        "an ordinary file server. \n" +
        "If the fourth argument is true,it will require\n" +
        "client authentication as well.");

    int port = DefaultServerPort;
    String docroot = "";

    if (args.length >= 1) {
        port = Integer.parseInt(args[0]);
    }

    if (args.length >= 2) {
        docroot = args[1];
    }
    String type = "PlainSocket";
    if (args.length >= 3) {
        type = args[2];
    }
    try {
        ServerSocketFactory ssf =
        ClassFileServer.getServerSocketFactory(type);
        ServerSocket ss = ssf.createServerSocket(port);
        if (args.length >= 4 && args[3].equals("true")) {
        ((SSLServerSocket)ss).setNeedClientAuth(true);
        }
        new ClassFileServer(ss, docroot);
    } catch (IOException e) {
        System.out.println("Unable to start ClassServer: " +
                 e.getMessage());
        e.printStackTrace();
    }
    }

    private static ServerSocketFactory getServerSocketFactory(String type) {
    if (type.equals("TLS")) {
        SSLServerSocketFactory ssf = null;
        try {
        // set up key manager to do server authentication
        SSLContext ctx;
        KeyManagerFactory kmf;
        KeyStore ks;
        char[] passphrase = "passphrase".toCharArray();

        ctx = SSLContext.getInstance("TLS");
        kmf = KeyManagerFactory.getInstance("SunX509");
        ks = KeyStore.getInstance("JKS");

        ks.load(new FileInputStream("testkeys"), passphrase);
        kmf.init(ks, passphrase);
        ctx.init(kmf.getKeyManagers(), null, null);

        ssf = ctx.getServerSocketFactory();
        return ssf;
        } catch (Exception e) {
        e.printStackTrace();
        }
    } else {
        return ServerSocketFactory.getDefault();
    }
    return null;
    }
}

Code ClassServer.java
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.io.*;
import java.net.*;
import javax.net.*;

/*
 * ClassServer.java -- a simple file server that can serve
 * Http get request in both clear and secure channel
 */

/**
 * Based on ClassServer.java in tutorial/rmi
 */
public abstract class ClassServer implements Runnable {

    private ServerSocket server = null;
    /**
     * Constructs a ClassServer based on <b>ss</b> and
     * obtains a file's bytecodes using the method <b>getBytes</b>.
     *
     */
    protected ClassServer(ServerSocket ss)
    {
    server = ss;
    newListener();
    }

    /**
     * Returns an array of bytes containing the bytes for
     * the file represented by the argument <b>path</b>.
     *
     * @return the bytes for the file
     * @exception FileNotFoundException if the file corresponding
     * to <b>path</b> could not be loaded.
     * @exception IOException if error occurs reading the class
     */
    public abstract byte[] getBytes(String path)
    throws IOException, FileNotFoundException;

    /**
     * The "listen" thread that accepts a connection to the
     * server, parses the header to obtain the file name
     * and sends back the bytes for the file (or error
     * if the file is not found or the response was malformed).
     */
    public void run()
    {
    Socket socket;

    // accept a connection
    try {
        socket = server.accept();
    } catch (IOException e) {
        System.out.println("Class Server died: " + e.getMessage());
        e.printStackTrace();
        return;
    }

    // create a new thread to accept the next connection
    newListener();

    try {
        DataOutputStream out =
        new DataOutputStream(socket.getOutputStream());
        try {
        // get path to class file from header
        BufferedReader in =
            new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String path = getPath(in);
        // retrieve bytecodes
        byte[] bytecodes = getBytes(path);
        // send bytecodes in response (assumes HTTP/1.0 or later)
        try {
            out.writeBytes("HTTP/1.0 200 OK\r\n");
            out.writeBytes("Content-Length: " + bytecodes.length +
                   "\r\n");
            out.writeBytes("Content-Type: text/html\r\n\r\n");
            out.write(bytecodes);
            out.flush();
        } catch (IOException ie) {
            ie.printStackTrace();
            return;
        }

        } catch (Exception e) {
        e.printStackTrace();
        // write out error response
        out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");
        out.writeBytes("Content-Type: text/html\r\n\r\n");
        out.flush();
        }

    } catch (IOException ex) {
        // eat exception (could log error to log file, but
        // write out to stdout for now).
        System.out.println("error writing response: " + ex.getMessage());
        ex.printStackTrace();

    } finally {
        try {
        socket.close();
        } catch (IOException e) {
        }
    }
    }

    /**
     * Create a new thread to listen.
     */
    private void newListener()
    {
    (new Thread(this)).start();
    }

    /**
     * Returns the path to the file obtained from
     * parsing the HTML header.
     */
    private static String getPath(BufferedReader in)
    throws IOException
    {
    String line = in.readLine();
    String path = "";
    // extract class from GET line
    if (line.startsWith("GET /")) {
        line = line.substring(5, line.length()-1).trim();
        int index = line.indexOf(' ');
        if (index != -1) {
            path = line.substring(0, index);
        }
    }

    // eat the rest of header
    do {
        line = in.readLine();
    } while ((line.length() != 0) &&
           (line.charAt(0) != '\r') && (line.charAt(0) != '\n'));

    if (path.length() != 0) {
        return path;
    } else {
        throw new IOException("Malformed Header");
    }
    }
}

It’s nice to be important but it’s more important to be nice


  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Staat de . (huidige directory) wel in je classpath?

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • roelio
  • Registratie: Februari 2001
  • Niet online

roelio

fruitig, en fris.

Omdat het compilen niet fout gaat maar het een runtime error is, lijkt het er inderdaad op dat het bijv. met het classpath te maken heeft. Heb je alle bestanden in dezelfde map staan? Bestaat ClassFileServer.class?

AMD Phenom II X4 // 8 GB DDR2 // SAMSUNG 830 SSD // 840 EVO SSD // Daar is Sinterklaas alweer!!


  • pjonk
  • Registratie: November 2000
  • Laatst online: 29-12-2025
Hmm laat ik even wat specifieker zijn:
De JAR files zijn:
- D:\docs\jsse1.0.2\lib\jcert.jar
- D:\docs\jsse1.0.2\lib\jnet.jar
- D:\docs\jsse1.0.2\lib\jsse.jar

Mijn .java files staan in C:\Applets\SSL
en mijn gecompileerde .class files staan in C:\Applets\SSL\Classes

Vanuit de directory C:\Applets\SSL\Classes voerde ik:
JAVA ClassFileServer uit.

Wat moet ik nu precies in mijn CLASSPATH environment variabele gaan zetten?

It’s nice to be important but it’s more important to be nice


  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
JonkieXL: Wat moet ik nu precies in mijn CLASSPATH environment variabele gaan zetten?
Alles ;) .

Je moet ervoor zorgen dat in ieder geval de . in de classpath staat. Normaal gesproken staat deze er standaard in, maar als ik de melding van jou zie zou dit toch wel eens het probleem kunnen zijn. Verder moeten die jarretjes ook allemaal in de classpath staan. Hoe je dit moet aangeven verschilt per OS... Je kunt ook direct in je Java commando de classpath opgeven.

Misschien kan je die SSL libs beter bij de standaard extenties van de JRE zetten. Dan hoef je ze namelijk niet in de classpath te zetten. Deze worden dan automagisch meegenomen.

Welke Java versie gebruik je?

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Als je trouwens 1.4.0 beta 3 gebruikt ben je helemaal van je problemen af: daar zitten JSSE standaard al in.

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • roelio
  • Registratie: Februari 2001
  • Niet online

roelio

fruitig, en fris.

Op dinsdag 08 januari 2002 12:17 schreef mbravenboer het volgende:
Als je trouwens 1.4.0 beta 3 gebruikt ben je helemaal van je problemen af: daar zitten JSSE standaard al in.
:Y)

AMD Phenom II X4 // 8 GB DDR2 // SAMSUNG 830 SSD // 840 EVO SSD // Daar is Sinterklaas alweer!!


  • pjonk
  • Registratie: November 2000
  • Laatst online: 29-12-2025
Op dinsdag 08 januari 2002 12:16 schreef mbravenboer het volgende:

[..]

Alles ;) .

Je moet ervoor zorgen dat in ieder geval de . in de classpath staat. Normaal gesproken staat deze er standaard in, maar als ik de melding van jou zie zou dit toch wel eens het probleem kunnen zijn.
Thanks dat was het probleem het werkt nu goed :)

It’s nice to be important but it’s more important to be nice

Pagina: 1