-
Java webserver.
Hello!
I'm writing a webserver in java and i have encountered a problem, the webserver works when i run it in netbeans, but when i make it into a .jar file and run it it doesn't work!? the resources is inside of the .jar file (/pytteweb/resources/) and the program is called pytteweb, what am i doing wrong? The code to locate the resources is:
Code:
String path = null;
URL url = this.getClass().getResource("/pytteweb/resources/");
try {
path = url.getPath();
} catch (Exception e) {
}
the path is when the .jar file is run: file:/C:/Users/Michael/Documents/Skola/2011/Datakommunikation/webserver_test/pytteweb/dist/pytteweb.jar!/pytteweb/resources/
the path is when it's run in netbeans: /C:/Users/Michael/Documents/Skola/2011/Datakommunikation/webserver_test/pytteweb/build/classes/pytteweb/resources/
please help me, i've been working with this for weeks now!!
-
Re: Java webserver.
What exception are you getting?
What are you trying to do when you get that exception?
-
Re: Java webserver.
Hello and thanks for responding!
I'm not getting any exeptiosn at all!? but when i run the program as a .jar file the server isn't loading any pages, as it does when i run it in netbeans!
-
Re: Java webserver.
How do you know?
Remember, we haven't a clue what this is supposed to be doing.
You;re question is essentially "it doesn't work", but you haven't explained what it should do.
-
Re: Java webserver.
Ah! Because i check the path variable with a JFrame in different parts of the program. Ok, the part of the program that i belive is not working is supposed to locate the resources folder inside the pytteweb.jar file, so the pytteweb webserver can load the files from ~/pytteweb/resources/ and using a DataOutputStream display them on the clients webclient. This works when the program is run in the editor, but not when running the .jar file.
The code for the method is:
Code:
public void handleClientRequest() throws IOException {
String path = null;
URL url = this.getClass().getResource("/pytteweb/resources/");
try {
path = url.getPath();
} catch (Exception e) {
JFrame framee = new JFrame("path error test");
Container panee = framee.getContentPane();
JLabel labele = new JLabel(path + " not created, error: " + e);
panee.add(labele);
framee.pack();
framee.setVisible(true);
}
JFrame frame0 = new JFrame("path test");
Container pane0 = frame0.getContentPane();
JLabel label0 = new JLabel(path + " is the path");
pane0.add(label0);
frame0.pack();
frame0.setVisible(true);
DataOutputStream output = new DataOutputStream(mSocket.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
String header = input.readLine();
String temp = header;
header = header.toUpperCase();
if (header.startsWith("GET")) {
int requestStart = 0;
int requestEnd = 0;
for (int a = 0; a < temp.length(); a++) {
if (temp.charAt(a) == ' ' && requestStart != 0) {
requestEnd = a;
break;
}
if (temp.charAt(a) == ' ' && requestStart == 0) {
requestStart = a;
}
}
path = path + temp.substring(requestStart + 2, requestEnd);
System.out.println(path);
this.sendFile(output, path);
}
else if (header.startsWith("HEAD")) {
String message = null;
File theFile = new File(path);
output.writeUTF(this.http_message(200));
output.writeUTF("Content-length: " + theFile.length() + "\n");
output.writeUTF("Content-Type: ");
if (path.endsWith("html") || path.endsWith("htm")) {
output.writeUTF("text/html");
} else if (path.endsWith("java")) {
output.writeUTF("text/plain");
} else if (path.endsWith("jpeg") || path.endsWith("jpg") || path.endsWith("gif") || path.endsWith("png")) {
output.writeUTF("image/gif");
} else {
output.writeUTF("text/html");
}
output.writeUTF("\n");
} else {
output.writeUTF(this.http_message(400));
}
output.flush();
}
-
Re: Java webserver.
A JFrame?
Seems a bit of overkill.
Why not simply log it, or even Sysout.println()?
By trying to monitor the code via Swing you are opening up a load of other potential failure points that wouldn't exist by simply println()ing.
-
Re: Java webserver.
Yes, i use the JFrame for debugging purposes, it's not going to be in the final version of the program.
-
Re: Java webserver.
Well, the first thing I would do is take that out and replace it with some normal debugging.
-
Re: Java webserver.
Yes, but i had the same problem with my program before i put in the windows, so ramoving the windows won't change anything! The program doesn't load the .html files in the resource folder inside of the pytteweb.jar file!
-
Re: Java webserver.
So, use some proper logging then copy and paste the logged output here so we can at least see where in the code it is going.
Without that we have nothing to go on.
JFrames are a terrible debugging solution because you cannot easily copy and paste the output of a run through your code, especially if each LogJFrame is a separate window.
This goes back to "How do you know".