Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Need help w. Java Networking.

Reply
Thread Tools

Need help w. Java Networking.

 
 
Steve R. Burrus
Guest
Posts: n/a
 
      06-05-2004
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://yourHostNameort/"
+"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 "";

}

} "







 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-05-2004
On Sat, 05 Jun 2004 20:58:27 GMT, "Steve R. Burrus"
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
mromarkhan@rogers.com
Guest
Posts: n/a
 
      06-06-2004
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.
 
Reply With Quote
 
Steve R. Burrus
Guest
Posts: n/a
 
      06-06-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) 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??


 
Reply With Quote
 
David R. Conrad
Guest
Posts: n/a
 
      06-06-2004
Roedy Green wrote:
> On Sat, 05 Jun 2004 20:58:27 GMT, "Steve R. Burrus"
> <(E-Mail Removed)> 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.
 
Reply With Quote
 
mromarkhan@rogers.com
Guest
Posts: n/a
 
      06-06-2004


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
 
Reply With Quote
 
Steve R. Burrus
Guest
Posts: n/a
 
      06-06-2004
(E-Mail Removed) 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"???

 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      06-06-2004
"Steve R. Burrus" <(E-Mail Removed)> wrote in message
news:RWKwc.4535$(E-Mail Removed) 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.


 
Reply With Quote
 
Steve R. Burrus
Guest
Posts: n/a
 
      06-07-2004
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??

 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      06-07-2004
"Steve R. Burrus" <(E-Mail Removed)> wrote in message
news:KgQwc.3176$(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Java Developer / Sr. Java Developer for Assyst @ Cochin/Trichy Assyst Java 3 09-24-2010 10:44 PM
Need a Sr Java Developer(Core Java) | Irvine, CA kin Java 0 10-23-2009 01:45 PM
Help Help, I am intermediate in Java...need help in follow case ElementX Java 9 10-01-2008 08:02 PM
Perl Pro but Java Newbie: Need nudge in proper direction for myfavorite Perl routine in Java /usr/ceo Java 32 09-15-2008 12:12 AM
re_---need help Network Adapters!!!! NEED HELP!!!! hedayatniac@gmail.com Computer Support 4 08-13-2006 01:03 AM



Advertisments