HELP!!! Problem importing com.orielly.servlet package...

I am trying to write a chat application that uses some classes in the "com.oreilly.servlet" package.  This package was downloaded as a zip file containing the package contents.  I renamed the zip file to a .jar file and placed it in this directory:  c:/jdk1.3/lib.  Problem is, that any program that has the import statement "import com.oreilly.servlet.*;" dosen't recogize the package or its classes.  I even tried unzipping the file to the same directory listed above and that didn't help either.  I added the directory: c:/jdk1.3/lib to my environment classpath variable, and even added an entry for the jar file in my path.  Nothing seems to work.

  Here is the location where I got the package (http://www.servlets.com/cos/index.html).  This package contains lots of helper classes for servlets, applet-servlet communication, amoung other things...  Please help!!!!!!!!!

JDC0724
LVL 1
jdc0724Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
bobbit31Connect With a Mentor Commented:
no, you have to unzip the file... there is a bunch of other stuff in the zip file... all you need is the jar file within the zip
0
 
bobbit31Commented:
you must add the .jar file to your classpath
0
 
bobbit31Commented:
sorry, didn't read the question fully... i see you've already added it to your classpath
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bobbit31Commented:
just to avoid the obvious are you using \'s or /'s in your path?
0
 
Jim CakalicSenior Developer/ArchitectCommented:
You need to add the full pathname to the jar/zip file to your CLASSPATH. For example, assuming the jar file is now named 'oreilly.jar' you would use something like:

    set CLASSPATH=%CLASSPATH%;C:\jdk1.3\lib\oreilly.jar

Best regards,
Jim Cakalic
0
 
jdc0724Author Commented:
I did add the full path to the jar.  Here is what I put in the class path variable:  c:\jdk1.3\lib\oreilly.jar;  The file originally was downloaded by a different name.  I renamed it to oreilly.jar and added it to the classpath and am still experiencing the same problems.

Thanks for your assistance.  This one is probably a little oversight on my part but pretty frustrating.  Thanks again for any assistance that can be provided.

JDC
0
 
jdc0724Author Commented:
I did add the full path to the jar.  Here is what I put in the class path variable:  c:\jdk1.3\lib\oreilly.jar;  The file originally was downloaded by a different name.  I renamed it to oreilly.jar and added it to the classpath and am still experiencing the same problems.

Thanks for your assistance.  This one is probably a little oversight on my part but pretty frustrating.  Thanks again for any assistance that can be provided.

JDC
0
 
bobbit31Commented:
restart your computer???
0
 
jdc0724Author Commented:
Yup, did that.  
0
 
bobbit31Commented:
does this work??

javac -classpath %CLASSPATH%;"c:\jdk1.3\lib\oreilly.jar" <filename>

try using a : instead of ; above as well (i do all my java stuff on a linux box and my dos is getting a little weak) ;)
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Since you're using servlets I would assume that you are using an app server or some servlet engine like Tomcat. Many of these have their own ideas -- independent of Win32 environment or command-line environment -- about the definition of classpath. Which app server or servlet engine are you using?

Jim
0
 
jdc0724Author Commented:
Bobbit31, the ";" is the correct representation.  

Jim, I am using apache and tomcat for my application.  However, at this point in time, I am just trying to compile an applet on my local drive.  I am using a basic editor (real J) to do the compile.  I figured there would be no sense trying to set this up on apache/tomcat until I could atleast compile.  I am going to include the applet code below.  Could someone try downloading the .jar file I listed above from the link I listed above and see if you can get this applet to compile.  Or atleast past the problem with importing this package???  It has got me stumped.

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import com.oreilly.servlet.HttpMessage;

public class HttpChatApplet extends Applet implements Runnable {

  TextArea text;
  Label label;
  TextField input;
  Thread thread;
  String user;

  public void init() {
    // Check if this applet was loaded directly from the filesystem.
    // If so, explain to the user that this applet needs to be loaded
    // from a server in order to communicate with that server's servlets.
    URL codebase = getCodeBase();
    if (!"http".equals(codebase.getProtocol())) {
      System.out.println();
      System.out.println("*** Whoops! ***");
      System.out.println("This applet must be loaded from a web server.");
      System.out.println("Please try again, this time fetching the HTML");
      System.out.println("file containing this servlet as");
      System.out.println("\"http://server:port/file.html\".");
      System.out.println();
      System.exit(1);  // Works only from appletviewer
                       // Browsers throw an exception and muddle on
    }

    // Get this user's name from an applet parameter set by the servlet
    // We could just ask the user, but this demonstrates a
    // form of servlet->applet communication.
    user = getParameter("user");
    if (user == null) user = "anonymous";

    // Set up the user interface...
    // On top, a large TextArea showing what everyone's saying.
    // Underneath, a labeled TextField to accept this user's input.
    text = new TextArea();
    text.setEditable(false);
    label = new Label("Say something: ");
    input = new TextField();
    input.setEditable(true);

    setLayout(new BorderLayout());
    Panel panel = new Panel();
    panel.setLayout(new BorderLayout());

    add("Center", text);
    add("South", panel);

    panel.add("West", label);
    panel.add("Center", input);
  }

  public void start() {
    thread = new Thread(this);
    thread.start();
  }

  String getNextMessage() {
    String nextMessage = null;
    while (nextMessage == null) {
      try {
        URL url = new URL(getCodeBase(), "/servlet/ChatServlet");
        HttpMessage msg = new HttpMessage(url);
        InputStream in = msg.sendGetMessage();
        DataInputStream data = new DataInputStream(
                               new BufferedInputStream(in));
        nextMessage = data.readLine();
      }
      catch (SocketException e) {
        // Can't connect to host, report it and wait before trying again
        System.out.println("Can't connect to host: " + e.getMessage());
        try { Thread.sleep(5000); } catch (InterruptedException ignored) { }
      }
      catch (FileNotFoundException e) {
        // Servlet doesn't exist, report it and wait before trying again
        System.out.println("Resource not found: " + e.getMessage());
        try { Thread.sleep(5000); } catch (InterruptedException ignored) { }
      }
      catch (Exception e) {
        // Some other problem, report it and wait before trying again
        System.out.println("General exception: " +
          e.getClass().getName() + ": " + e.getMessage());
        try { Thread.sleep(1000); } catch (InterruptedException ignored) { }
      }
    }
    return nextMessage + "\n";
  }

  public void run() {
    while (true) {
      text.appendText(getNextMessage());
    }
  }

  public void stop() {
    thread.stop();
    thread = null;
  }

  void broadcastMessage(String message) {
    message = user + ": " + message;  // Pre-pend the speaker's name
    try {
      URL url = new URL(getCodeBase(), "/servlet/ChatServlet");
      HttpMessage msg = new HttpMessage(url);
      Properties props = new Properties();
      props.put("message", message);
      msg.sendPostMessage(props);
    }
    catch (SocketException e) {
      // Can't connect to host, report it and abandon the broadcast
      System.out.println("Can't connect to host: " + e.getMessage());
    }
    catch (FileNotFoundException e) {
      // Servlet doesn't exist, report it and abandon the broadcast
      System.out.println("Resource not found: " + e.getMessage());
    }
    catch (Exception e) {
      // Some other problem, report it and abandon the broadcast
      System.out.println("General exception: " +
        e.getClass().getName() + ": " + e.getMessage());
    }
  }

  public boolean handleEvent(Event event) {
    switch (event.id) {
      case Event.ACTION_EVENT:
        if (event.target == input) {
          broadcastMessage(input.getText());
          input.setText("");
          return true;
        }
    }
    return false;
  }
}
0
 
bobbit31Commented:
jim makes a good point, are you having compile problems or problems when calling the servlet?
0
 
bobbit31Commented:
hrmm,

are you sure that the jar file is in fact in c:\jdk1.3\lib ???

i extracted the jar file and at first had problems but that was b/c it extracted to c:\jdk1.3\lib\lib\cos.jar

i used this:
javac -classpath %CLASSPATH%;"c:\jdk1.3\lib\lib\cos.jar" HttpChatApplet.java

and it worked (although you are using a deprecated api)
0
 
bobbit31Commented:
now, if you are compiling from your editor rather than from the dos prompt then i know certain programs like (JCreator, which i use) have to be configured... for jcreator i know it's under configure/options/jdk profile

0
 
jdc0724Author Commented:
bobbit31, did you actually unzip the file, and then use the import statement in the applet?  Or did you do what I did, which is just renaming the .zip file to .jar?  

I am using realJ and couldn't find any options for adding jars.  The only option there is which JDK to use.  Which I have set to jdk1.3.  

Also, I did get the deprecated api message but didn't know what to do about it?  Is there some logic in the example that is using old java syntax?
0
 
bobbit31Commented:
as far as the deprecated message goes...  your stuff will still work, but the deprecated class/method could be taken out of future versions.
0
 
jdc0724Author Commented:
All, I did was rename the zip file to .jar since the web site I got the file from said it was a zip file, readable by .jar.  let me unzip it now.
0
 
jdc0724Author Commented:
Ok, I did it in dos and all I got was the deprecated message.  I had to place the java source file in the c:\jdk1.3\bin directory in order for it to compile however.

1. I unzipped into the lib directory and got the same problem you did.  C:jdk1.3\lib\lib\cos.jar.  Should I have unzipped to the root c:\jdk1.3 directory instead?  Or is this the way I was supposed to do it?

2. Earlier when we were talking I was on windows NT and added the classpath through control panel, system, environment, classpath.  Now, I am at home using Win98, where should I set the class path (since it dosen't have the option of doing it through the control panel) so I don't have to do it everytime??

3.  I am assuming I need to change my classpath and I am using win98 and Forte from home right now.  I am assuming I need to find a way for forte to see the .jar??? Or should it see it anyway since I am pointing to the .jdk.


Thanks,
JDC
0
 
Jim CakalicSenior Developer/ArchitectCommented:
I downloaded the cos-19Jun2001.zip. From the contents of the zip file, it seems evident to me that you cannot simply rename that .zip to a .jar extension and put it in your classpath. Once you have unzipped it, there is a cos.jar in the lib directory. You can move that .jar to C:\jdk1.3\lib and change your classpath to include C:\jdk1.3\lib\cos.jar.

Jim
0
 
bobbit31Commented:
1. it doesn't matter, you can always move the jar file wherever you want to.

2. autoexec.bat

3. so long as you set your classpath up correctly you should be ok... although i haven't used Forte so i don't know what problems you might run into there.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
As for when to unzip/add the jar -- it is, AFAIK, considered poor practice to put anything in the jdk/lib directory that wasn't there initially as part of the jdk.

Jim
0
 
jdc0724Author Commented:
Where should I have unzipped to then?  I want to conform to standards since I will  be using this exact same setup at work, not just at home.

I am also assuming that changing the autoexec.bat is the right thing to do?  I would hate to have to compile in dos everytime.  I cannot seem to find out how to change the classpath in forte.
0
 
jdc0724Author Commented:
Does this look correct for my autoexec.bat?

SET PATH=c:\Oracle\Ora81\bin;"C:\PROGRAM FILES\ORACLE\JRE\1.1.7\BIN";%PATH%;c:\jdk1.3\lib\cos.jar;
0
 
bobbit31Commented:
you need:
SET CLASSPATH="C:\jdk1.3\lib";"C:\jdk1.3\lib\cos.jar"; etc...

0
 
jdc0724Author Commented:
So we need a separate classpath variable instead of appending to the "path" variable?  Does this look better?

SET PATH=c:\Oracle\Ora81\bin;"C:\PROGRAM FILES\ORACLE\JRE\1.1.7\BIN";%PATH%;
SET CLASSPATH="C:\jdk1.3\lib";"C:\jdk1.3\lib\cos.jar";
0
 
bobbit31Commented:
yeah that's fine.
0
 
jdc0724Author Commented:
I am still curious about the location of the unzipped file. It creates a bunch of new folders (classes, doc, etc) and i wonder if I should of unzipped them at the root c:\jdk1.3 directory?

 If I had done that, the new directories would reside under the root jdk1.3 directory and the cos.jar would of been placed under the c:/jdk1.3/lib...  

I don't want to do this if it is considered poor practice but I wonder if that was the intention of the zip file?  I am going to reboot now and see if setting the classpath to the new .jar works.  I will check back in just a few minutes.
0
 
jdc0724Author Commented:
I don't think changing my autoexec bought me anything.  I imported the jar file into forte and the programs seem to be compiling now.  I haven't had a chance to run them yet.
0
 
blackCommented:
i think you make a slight mistake here, you first unzip everything then grab the cos.jar file that is in the lib directory created and then put that into your classpath
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.