Solved

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

Posted on 2001-08-29
30
288 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:jdc0724
  • 13
  • 12
  • 4
  • +1
30 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6437886
you must add the .jar file to your classpath
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6437891
sorry, didn't read the question fully... i see you've already added it to your classpath
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6437907
just to avoid the obvious are you using \'s or /'s in your path?
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6437911
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
 
LVL 1

Author Comment

by:jdc0724
ID: 6438051
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
 
LVL 1

Author Comment

by:jdc0724
ID: 6438053
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 6438069
restart your computer???
0
 
LVL 1

Author Comment

by:jdc0724
ID: 6438086
Yup, did that.  
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6438130
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
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6438137
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
 
LVL 1

Author Comment

by:jdc0724
ID: 6438225
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 6438230
jim makes a good point, are you having compile problems or problems when calling the servlet?
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6438261
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 6438280
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
 
LVL 1

Author Comment

by:jdc0724
ID: 6438366
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 6438375
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 6438391
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
 
LVL 1

Author Comment

by:jdc0724
ID: 6438411
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
 
LVL 1

Author Comment

by:jdc0724
ID: 6438458
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
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6438473
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 6438474
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
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6438492
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
 
LVL 1

Author Comment

by:jdc0724
ID: 6438513
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
 
LVL 1

Author Comment

by:jdc0724
ID: 6438515
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 6438525
you need:
SET CLASSPATH="C:\jdk1.3\lib";"C:\jdk1.3\lib\cos.jar"; etc...

0
 
LVL 1

Author Comment

by:jdc0724
ID: 6438539
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 6438541
yeah that's fine.
0
 
LVL 1

Author Comment

by:jdc0724
ID: 6438549
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
 
LVL 1

Author Comment

by:jdc0724
ID: 6438603
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
 
LVL 3

Expert Comment

by:black
ID: 6439228
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now