Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Need help w. Java Networking. (http://www.velocityreviews.com/forums/t134125-need-help-w-java-networking.html)

Steve R. Burrus 06-05-2004 08:58 PM

Need help w. Java Networking.
 
I am trying to run this simple example of some Java Networking. The
example is called "httpServer.java", and I believe that it uses a HTTP
Server, very much like the Apache Server! I can compile it okay, but
when I go to running it, using the command "java httpServer [num. of the
HTTP Server port], I am afraid that I always get back this error ! :

"java.lang.NoClassDefFoundError: C:\MyJavaFiles\httpServer/java
Exception in thread "main" " Here is the entire code for the program.
What could be wrong with it?

"// a simple example of some Java Networking.

import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;


public class httpServer
{

public static void main(String args[]) {

int port;
ServerSocket server_socket;


try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
port = 1500;
}


try {

server_socket = new ServerSocket(port);
System.out.println("httpServer running on port " +
server_socket.getLocalPort());

// server infinite loop
while(true) {
Socket socket = server_socket.accept();
System.out.println("New connection accepted " +
socket.getInetAddress() +
":" + socket.getPort());

// Construct handler to process the HTTP request message.
try {
httpRequestHandler request =
new httpRequestHandler(socket);
// Create a new thread to process the request.
Thread thread = new Thread(request);

// Start the thread.
thread.start();
}
catch(Exception e) {
System.out.println(e);
}
}
}

catch (IOException e) {
System.out.println(e);
}
}
}



class httpRequestHandler implements Runnable
{
final static String CRLF = "\r\n";
Socket socket;
InputStream input;
OutputStream output;
BufferedReader br;

// Constructor
public httpRequestHandler(Socket socket) throws Exception
{
this.socket = socket;
this.input = socket.getInputStream();
this.output = socket.getOutputStream();
this.br =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
}

// Implement the run() method of the Runnable interface.
public void run()
{
try {
processRequest();
}
catch(Exception e) {
System.out.println(e);
}
}

private void processRequest() throws Exception
{
while(true) {

String headerLine = br.readLine();
System.out.println(headerLine);
if(headerLine.equals(CRLF) || headerLine.equals("")) break;

StringTokenizer s = new StringTokenizer(headerLine);
String temp = s.nextToken();

if(temp.equals("GET")) {

String fileName = s.nextToken();
fileName = "." + fileName ;

// Open the requested file.
FileInputStream fis = null ;
boolean fileExists = true ;
try
{
fis = new FileInputStream( fileName ) ;
}
catch ( FileNotFoundException e )
{
fileExists = false ;
}

// Construct the response message.
String serverLine = "Server: fpont simple java httpServer";
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
String contentLengthLine = "error";
if ( fileExists )
{
statusLine = "HTTP/1.0 200 OK" + CRLF ;
contentTypeLine = "Content-type: " +
contentType( fileName ) + CRLF ;
contentLengthLine = "Content-Length: "
+ (new Integer(fis.available())).toString()
+ CRLF;
}
else
{
statusLine = "HTTP/1.0 404 Not Found" + CRLF ;
contentTypeLine = "text/html" ;
entityBody = "<HTML>" +
"<HEAD><TITLE>404 Not Found</TITLE></HEAD>" +
"<BODY>404 Not Found"
+"<br>usage:http://yourHostName:port/"
+"fileName.html</BODY></HTML>" ;
}

// Send the status line.
output.write(statusLine.getBytes());

// Send the server line.
output.write(serverLine.getBytes());

// Send the content type line.
output.write(contentTypeLine.getBytes());

// Send the Content-Length
output.write(contentLengthLine.getBytes());

// Send a blank line to indicate the end of the header lines.
output.write(CRLF.getBytes());

// Send the entity body.
if (fileExists)
{
sendBytes(fis, output) ;
fis.close();
}
else
{
output.write(entityBody.getBytes());
}

}
}

try {
output.close();
br.close();
socket.close();
}
catch(Exception e) {}
}

private static void sendBytes(FileInputStream fis, OutputStream os)
throws Exception
{
// Construct a 1K buffer to hold bytes on their way to the socket.
byte[] buffer = new byte[1024] ;
int bytes = 0 ;

// Copy requested file into the socket's output stream.
while ((bytes = fis.read(buffer)) != -1 )
{
os.write(buffer, 0, bytes);
}
}

private static String contentType(String fileName)
{
if (fileName.endsWith(".htm") || fileName.endsWith(".html"))
{
return "text/html";
}

return "";

}

} "








Roedy Green 06-05-2004 09:04 PM

Re: Need help w. Java Networking.
 
On Sat, 05 Jun 2004 20:58:27 GMT, "Steve R. Burrus"
<burrus1@swbell.net> wrote or quoted :

>NoClassDefFoundError


the exception that makes grow men pale.

http://mindprod.com/jgloss/errormess...FOUNDEXCEPTION

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

mromarkhan@rogers.com 06-06-2004 02:46 AM

Re: Need help w. Java Networking.
 
Peace.

Steve R. Burrus wrote:
> "java.lang.NoClassDefFoundError: C:\MyJavaFiles\httpServer/java
> Exception in thread "main" " Here is the entire code for the program.

[snip][snip]

I guess you are appending .java to the end.

Reproducable test case:
C:\My Documents\Omar\nntp-test\Java_1_1>java Client.java
Exception in thread "main" java.lang.NoClassDefFoundError: Client/java

just a guess.

Have a good day.

Steve R. Burrus 06-06-2004 05:53 AM

Re: Need help w. Java Networking.
 
mromarkhan@rogers.com wrote:

>I guess you are appending .java to the end.


No, I can assure you that I have been working with the Java Programming
language long enough to NOT dare do that, what you imply!!! Someone
recently in this group said to put a -cp switch with the java
interpreter command, and it DID allow/permit me to see a particular Java
program. Should I do it here with this Java networking program??



David R. Conrad 06-06-2004 11:53 AM

Re: Need help w. Java Networking.
 
Roedy Green wrote:
> On Sat, 05 Jun 2004 20:58:27 GMT, "Steve R. Burrus"
> <burrus1@swbell.net> wrote or quoted :
>
>>NoClassDefFoundError

>
> the exception that makes grown men pale.
>


And pale men groan.

--
No, his mind is not for rent to any god or government.
Please direct replies to drc -at- asus -dot- net.

mromarkhan@rogers.com 06-06-2004 06:12 PM

Re: Need help w. Java Networking.
 


Peace be unto you

Steve R. Burrus wrote:
> Should I do it here with this Java networking program??


Sure, why not?

By the way, it works super for me.
Since your eXPerienced with Java programming,
I can not think of a reason why
it might not work for you.

But for the NOVICE who might stumble
upon this thread,

another reason
might be that is it non existant and the error is true

test case:
>java c:\MuFiles\me

java.lang.NoClassDefFoundError: c:\MuFiles\me
Exception in thread "main" >Exit code: 1
I do not have me.class in c:\MuFiles, let alone the directory


another reason
might be that it is part of a package.

test case:
package org.apachereplacement;
import java.net.*;
import java.io.*;
import java.util.*;
.........snip code

>javac httpServer.java
>Exit code: 0

java httpServer
>java httpServer

java.lang.NoClassDefFoundError: httpServer (wrong name: org/apachereplacement/httpServer)
.....
Exception in thread "main" >Exit code: 1
But of course, this produces a *different*
error message


Have a good day.


Appendix A - Output of nice http server class
>javac httpServer.java
>Exit code: 0
>java httpServer

httpServer running on port 1500
New connection accepted /127.0.0.1:1330
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-gsarcade-launch, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; .NET CLR 1.1.4322)
Host: localhost:1500
Connection: Keep-Alive

New connection accepted /127.0.0.1:1331
GET /httpServer.java HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-gsarcade-launch, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; .NET CLR 1.1.4322)
Host: localhost:1500
Connection: Keep-Alive


java httpServer 1500
>java httpServer 1500

httpServer running on port 1500


java httpServer 1200
>java httpServer 1200

httpServer running on port 1200

Steve R. Burrus 06-06-2004 08:14 PM

Re: Need help w. Java Networking.
 
mromarkhan@rogers.com wrote:
> another reason
> might be that it is part of a package.
>
> test case:
> package org.apachereplacement;
> import java.net.*;
> import java.io.*;
> import java.util.*;
> ........snip code
>>javac httpServer.java
>>Exit code: 0

>
> java httpServer
>
>>java httpServer

>
> java.lang.NoClassDefFoundError: httpServer (wrong name:

org/apachereplacement/httpServer)
> ....
> Exception in thread "main" >Exit code: 1
> But of course, this produces a *different*
> error message


Hey friend, thanx a lot for the most detailed bit of help/assistance
that I have received yet about this Java Networking problem of mine!!
Listen, you referred in your post to a possible package that my Java
file could be in. As a matter of fact, it IS in a package, something
like "com.stephen.burrus"!! So, after it's successfully compiled in DOS,
to run the program, I should probably refer to the entire package name
that the class file is in, like "java com\stephen\burrus\httpServer"???


Ryan Stewart 06-06-2004 08:42 PM

Re: Need help w. Java Networking.
 
"Steve R. Burrus" <burrus1@swbell.net> wrote in message
news:RWKwc.4535$WI3.3778@newssvr23.news.prodigy.co m...
> Hey friend, thanx a lot for the most detailed bit of help/assistance
> that I have received yet about this Java Networking problem of mine!!
> Listen, you referred in your post to a possible package that my Java
> file could be in. As a matter of fact, it IS in a package, something
> like "com.stephen.burrus"!! So, after it's successfully compiled in DOS,
> to run the program, I should probably refer to the entire package name
> that the class file is in, like "java com\stephen\burrus\httpServer"???
>

If it's in a package, you *must* run it like that. And class names should
start with a capital letter, like HttpServer.



Steve R. Burrus 06-07-2004 02:19 AM

STILL Need help w. Java Networking.
 
Ryan Stewart wrote:

> If it's in a package, you *must* run it like that. And class names should
> start with a capital letter, like HttpServer.


Well, I re-named the "httpServer.java" file to the "HttpServer.java"
file in DOS earlier, then tried my luck with running the Java intepreter
on it, but alas, this is what I STILL got back in the way of an error!!!

"C:\HMI\servletbible\ch04\examples>java
servletbible\ch04\examples\HttpServer 80
Exception in thread "main" java.lang.NoClassDefFoundError:
servletbible\ch04\examples\HttpServer"

And I also tried attaching the .class file extension to it, but that
didn't help any, so where am I going wrong now??


Ryan Stewart 06-07-2004 02:47 AM

Re: STILL Need help w. Java Networking.
 
"Steve R. Burrus" <burrus1@swbell.net> wrote in message
news:KgQwc.3176$qH1.1110@newssvr24.news.prodigy.co m...
> Ryan Stewart wrote:
>
> > If it's in a package, you *must* run it like that. And class names

should
> > start with a capital letter, like HttpServer.

>
> Well, I re-named the "httpServer.java" file to the "HttpServer.java"
> file in DOS earlier, then tried my luck with running the Java intepreter
> on it, but alas, this is what I STILL got back in the way of an error!!!
>
> "C:\HMI\servletbible\ch04\examples>java
> servletbible\ch04\examples\HttpServer 80
> Exception in thread "main" java.lang.NoClassDefFoundError:
> servletbible\ch04\examples\HttpServer"
>
> And I also tried attaching the .class file extension to it, but that
> didn't help any, so where am I going wrong now??
>

Question: Why are you bothering with trying to compile an HTTP server if you
don't even know the very basics of Java? I'd suggest you spend some time
here:
http://java.sun.com/docs/books/tutorial/

To answer your question, it looks like you should be running it from the
C:\HMI directory, assuming you've correctly identified the package as
servletbible.ch04.examples.

Finally, this kind of basic question belongs in comp.lang.java.help.




All times are GMT. The time now is 06:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.