Link to home
Start Free TrialLog in
Avatar of jdc0724
jdc0724

asked on

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
Avatar of bobbit31
bobbit31
Flag of United States of America image

you must add the .jar file to your classpath
sorry, didn't read the question fully... i see you've already added it to your classpath
just to avoid the obvious are you using \'s or /'s in your path?
Avatar of Jim Cakalic
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
Avatar of jdc0724
jdc0724

ASKER

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
Avatar of jdc0724

ASKER

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
restart your computer???
Avatar of jdc0724

ASKER

Yup, did that.  
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) ;)
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
Avatar of jdc0724

ASKER

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;
  }
}
jim makes a good point, are you having compile problems or problems when calling the servlet?
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)
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

Avatar of jdc0724

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
as far as the deprecated message goes...  your stuff will still work, but the deprecated class/method could be taken out of future versions.
Avatar of jdc0724

ASKER

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.
Avatar of jdc0724

ASKER

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
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
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.
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
Avatar of jdc0724

ASKER

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.
Avatar of jdc0724

ASKER

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;
you need:
SET CLASSPATH="C:\jdk1.3\lib";"C:\jdk1.3\lib\cos.jar"; etc...

Avatar of jdc0724

ASKER

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";
yeah that's fine.
Avatar of jdc0724

ASKER

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.
Avatar of jdc0724

ASKER

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.
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