Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

Problem Creating a Java Installed Extension

I'm trying to create a sample java installed extension based on the Sun Tutorial at:

http://java.sun.com/docs/books/tutorial/ext/TOC.html

I've created a class file called RectangleArea.java containing:

    import java.awt.*;
    public final class RectangleArea
    {
        public static int area( java.awt.Rectangle r )
        {
            return r.width * r.height;
        }
    }
 
I've compiled it into a class file and then JARed it into a file called area.jar, which I copied into the JAVA_HOME/jre/lib/ext directory.

Then I've created a java source file called AreaApp.java containing:

    import java.awt.*;
    public class AreaApp
    {
        public static void main( String[] args )
        {
            int width = 10;
            int height = 5;
            Rectangle r = new Rectangle(width, height);
            System.out.println( "The rectangle's area is "   +
                                RectangleArea.area( r ) );
        }
    }

I've compiled this file (with the RectangleArea.java class in the current directory) and then tried to run it using "java AreaApp", but I get a "NoClassDefFoundError" for "RectangleArea".

I'm not sure what else I'm supposed to do. According to the tutorial and the Extension docs, all I have to do is jar a class file and place it into the correct directory... I'm on Win2K using the 1.4 JDK.

Thanks,
Frank
0
magenta
Asked:
magenta
  • 13
  • 11
  • 10
  • +1
2 Solutions
 
magentaAuthor Commented:
The code works if I add the jar file to the classpath. However, one benefit of using installed extensions is so that you don't have to add it to the classpath.
0
 
CEHJCommented:
I haven't tried this myself, but since so far you haven't had a response, I'll take a guess. Try:


   package rectangle;

   import java.awt.*;
   public final class RectangleArea
   {
       public static int area( java.awt.Rectangle r )
       {
           return r.width * r.height;
       }
   }

and then in the calling class:

import rectangle.RectangleArea;
import java.awt.*;
   public class AreaApp
   {



0
 
objectsCommented:
Check that you put the jar in *all* 'ext' directories.
If you have the JDK and a JRE installed then you may have two 'ext' directories.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
magentaAuthor Commented:
I added a package, but it didn't help. Also, there is only one ext directory... :( Any ideas?
0
 
objectsCommented:
Should work then, I've never had a problem with it.
What's the path of the 'ext' directoy?
0
 
magentaAuthor Commented:
The ext directory is "C:\Java\jdk1.4.0\jre\lib\ext". Note that I've put other jars there in the past and they've worked fine (for example, the mysql client).
0
 
objectsCommented:
Reading you question again it appears AreaApp compiles correctly, but does not run. Is this correct?
0
 
magentaAuthor Commented:
Yes, I can compile AreaApp.java, but the only way I can do that is to put the class file for RectangleArea in the current working directory (event though I would think the compiler would find it in the ext directory).

Here is the runtime error when I run AreaApp with the jar in the ext directory:

    java AreaApp
    java.lang.NoClassDefFoundError: frank/RectangleArea
        at AreaApp.main(AreaApp.java:12)
    Exception in thread "main"

By the way, I am creating the jar file by running:

    jar cvf area.jar RectangleArea.class

Is that correct or is there something else I need?

Thanks,
Frank
0
 
objectsCommented:
Frank,

That all looks fine.
Have you tried compiling AreaApp w/out having RectangleArea in the same directory?
0
 
magentaAuthor Commented:
Yes, I've tried compiling AreaApp without RectangleArea in the same directory (or on the classpath) and it does not compile. Here's the error:

    javac AreaApp.java
    AreaApp.java:3: package frank does not exist
    import frank.RectangleArea;
                 ^
    AreaApp.java:13: cannot access RectangleArea
    bad class file: C:\Java\jdk1.4.0\jre\lib\ext\area.jar(RectangleArea.class)
    class file contains wrong class: frank.RectangleArea
    Please remove or make sure it appears in the correct     subdirectory of the classpath.
                                RectangleArea.area( r ) );
                            ^
    2 errors

Is there a way to compile AreaApp without these errors?

Thanks,
Frank
0
 
objectsCommented:
Frank,

It appears to be finding the jar correctly in the ext directory, but it either doesn't have the package defined correctly, or it is in the wrong directory in the jar file.
0
 
CEHJCommented:
Yes, these things are usually to do with where you are in the file system when you jar/compile etc. This is what I'd do:

1. I have a 'projects' subdirectory under JAVA_HOME
2. Create a specific project directory under this, e.g. RectangleArea
3. Change to this directory. So you're now at:
JAVA_HOME/projects/RectangleArea
4. Create a package subdirectory e.g. 'frank'
  mkdir frank
5. Make RectangleArea in the frank (package subdirectory):

package frank;

import java.awt.*;

public final class RectangleArea {

     public static int area(Rectangle r) {
          return r.width * r.height;
     }
}

This code is now in JAVA_HOME/projects/RectangleArea/frank
6. Compile it:
javac frank/RectangleArea.java
7. Jar it:
jar -cvf frank/*.class
8. Delete the original class file so that you can be sure the application's using the jar
9. Move jar to JAVA_HOME/jre/lib/ext
10. Create a test application in JAVA_HOME/projects/RectangleArea

import frank.RectangleArea;
import java.awt.Rectangle;

class RectangleTest extends java.lang.Object {

     public static void main(String[] args) {
          Rectangle r = new Rectangle(100,100);
          System.out.println(RectangleArea.area(r));
     }
}

11. Run it:
java RectangleTest
 
0
 
CEHJCommented:
Of course if you're in Windows, adjust the file separators in the above to backslashes.
0
 
magentaAuthor Commented:
Ok, following CEHJ's instructions step by step, I found one thing wrong, when I created the jar file, I didn't have it in the package path. So now I am able to compile the test class without error. However, when I try to run the test class, I still get the error:

    java.lang.NoClassDefFoundError: frank/RectangleArea
     at AreaApp.main(AreaApp.java:12)
    Exception in thread "main"

Which is strange...why would it find it for compiling but not at run time?

Thanks,
Frank
0
 
CEHJCommented:
OK. Can you confirm that when you do:

jar -tvf %JAVA_HOME%\jre\lib\ext\frank.jar

at the command line you can see frank\RectangleArea.class in there?
0
 
magentaAuthor Commented:
First off, I called it "area.jar" not "frank.jar". That shouldn't be a problem right? Here's the output from the command above:

    C:\>jar -tvf %JAVA_HOME%\jre\lib\ext\area.jar
     0 Mon Jul 29 17:49:14 PDT 2002 META-INF/
    68 Mon Jul 29 17:49:14 PDT 2002 META-INF/MANIFEST.MF
   352 Mon Jul 29 17:48:28 PDT 2002 frank/RectangleArea.class

Thanks,
Frank
0
 
objectsCommented:
> why would it find it for compiling but not at run time?

Because there may be tow ext directories as I mentioned above.

Have you searched your file system for all 'ext' directories?

Maybe something like c:\Program Files\javasoft\jre\1.4\lib\ext
0
 
magentaAuthor Commented:
Here are all the other "ext" directories I could find. All of them contain HTML docs (in fact, they are all under the "doc" directory).

    C:\Java\docs\api\org\xml\sax\ext
    C:\Java\jdk1.4.0\docs\api\org\xml\sax\ext
    C:\Java\JWSDP\jwsdp-1_0\docs\api\org\xml\sax\ext
    C:\Java\xerces\xerces-1_4_4\docs\html\apiDocs\org\xml\sax\ext

Thanks,
Frank
0
 
CEHJCommented:
To eliminate the potential problem mentioned by Objects

do the following:

java -Djava.ext.dirs=%JAVA_HOME%\jre\lib\ext <classname>

Are you sure you're in the right directory? Where is the main class?
Where are you in relation to it?
0
 
magentaAuthor Commented:
Ok, this is strange. If I add the "-Djava.ext.dirs" argument from above, it works. Here's the output from the command line. I do everything from the directory with there test app lives. Could there be an environment variable that is messing with the default?

Thanks,
Frank

c:\Java\Training\Extensions\AreaUser>javac AreaApp.java
javac AreaApp.java

c:\Java\Training\Extensions\AreaUser>java AreaApp
java AreaApp
java.lang.NoClassDefFoundError: frank/RectangleArea
     at AreaApp.main(AreaApp.java:12)
Exception in thread "main"
c:\Java\Training\Extensions\AreaUser>java -Djava.ext.dirs=%JAVA_HOME%\jre\lib\ext AreaApp
java -Djava.ext.dirs=%JAVA_HOME%\jre\lib\ext AreaApp
The rectangle's area is 50

c:\Java\Training\Extensions\AreaUser>
0
 
CEHJCommented:
This suggests that you could have either the problem Objects mentioned, or the reverse, i.e. too *few* ext directories!
Compile and run this to see what your ext dirs are:

import java.util.Enumeration;
import java.util.Properties;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

public class Props {

  public static void main(String[] args){
          Properties props = System.getProperties();
          if(args.length > 0){
               try {
                    PrintWriter out = new PrintWriter(new FileOutputStream(args[0]));
                    props.list(out);
                    out.flush();
                    out.close();
               }
               catch(IOException e){
                    e.printStackTrace();
               }
          }
          else {
               props.list(System.out);
          }
  }
}
0
 
magentaAuthor Commented:
son of a @#$%^! There is another ext directory! This is the relavant output from the program above:

  java.ext.dirs=C:\Program Files\Java\j2re1.4.0\lib\ext

Now why the hell did Sun go create an ext dir there??? Especially considering the documentation refers to the ext dir under the installation directory. Ugh! :( When I searched my hard drive for other ext dirs, I never though of searching anywhere else but under "c:\java" since that's where I installed everything...

Anyway, I threw the jar file into the directory above and now it works fine.

Duh!

Ok, so now my dilemma is, who gets the points?

Thanks,
Frank
0
 
magentaAuthor Commented:
By the way, the compiler is obviously considering the ext but the run time environment is not. You guys gotta admit that that's squirrelly...

Also, where does the environment "remember" it's properties? Is there a props file somewhere?

Thanks,
Frank
0
 
objectsCommented:
> Now why the hell did Sun go create an ext dir there???

Because you have two JVM's installed.
One with the JDK and the other with the JRE.

> Ok, so now my dilemma is, who gets the points?

Well I've been telling you it sounds like you had two 'ext' directories since my 1st comment :)

0
 
CEHJCommented:
>>Ok, so now my dilemma is, who gets the points?

Me and objects I'd say. Ask him what % he wants!

I'll come back with some more comments on your observations a bit later
0
 
objectsCommented:
> You guys gotta admit that that's squirrelly...

No, as mentioned above there are two different JVM's involved.
0
 
CEHJCommented:
There's also the possibility that both versions of java.exe are in your path, with the jre one appearing first. Am I right?
0
 
magentaAuthor Commented:
I'm not sure how the other JVM got installed, but I'm sure that I would have never allowed it to be installed anywhere us but under c:\java.

So how do I split the points?

Later,
Frank
0
 
objectsCommented:
> I'm not sure how the other JVM got installed

It is installed as part of the JDK installation.
By the looks it's installed in "C:\Program Files\Java\j2re1.4"
0
 
CEHJCommented:
>>So how do I split the points?

Well I had heard that point splitting had got easier but the way I do it it as follows:

1. Ask community support to delete the question, giving them the question url, as you're about to split the points.
2. Post a couple of new questions called 'points for x' where 'x' is our respective id.

What about a 30-20 split between Objects and me. I don't mind if he has more, as he *did* diagnose the problem correctly, but I supplied you with a few aids to knowledge!
0
 
objectsCommented:
> but I supplied you with a few aids to knowledge!

I thought points were awarded for answering the question.
According to that logic you can post helpful comments to already answered questions and expect a points split :)
0
 
CEHJCommented:
Well I'm not going to lose sleep over 20 points! Have them all :-)
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- split points between CEHJ and objects

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0
 
magentaAuthor Commented:
I requested that these points get split ages ago...
0
 
girionisCommented:
 Now you can split them too and you do not have to ask a moderator :-)
0
 
objectsCommented:
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 13
  • 11
  • 10
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now