Solved

Problem Creating a Java Installed Extension

Posted on 2002-07-24
36
299 Views
Last Modified: 2013-11-23
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
Comment
Question by:magenta
  • 13
  • 11
  • 10
  • +1
36 Comments
 

Author Comment

by:magenta
ID: 7176409
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 25 total points
ID: 7176559
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
 
LVL 92

Accepted Solution

by:
objects earned 25 total points
ID: 7178732
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
 

Author Comment

by:magenta
ID: 7179363
I added a package, but it didn't help. Also, there is only one ext directory... :( Any ideas?
0
 
LVL 92

Expert Comment

by:objects
ID: 7179377
Should work then, I've never had a problem with it.
What's the path of the 'ext' directoy?
0
 

Author Comment

by:magenta
ID: 7179419
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
 
LVL 92

Expert Comment

by:objects
ID: 7179431
Reading you question again it appears AreaApp compiles correctly, but does not run. Is this correct?
0
 

Author Comment

by:magenta
ID: 7179446
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
 
LVL 92

Expert Comment

by:objects
ID: 7179448
Frank,

That all looks fine.
Have you tried compiling AreaApp w/out having RectangleArea in the same directory?
0
 

Author Comment

by:magenta
ID: 7180839
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
 
LVL 92

Expert Comment

by:objects
ID: 7181620
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 7182056
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 7182062
Of course if you're in Windows, adjust the file separators in the above to backslashes.
0
 

Author Comment

by:magenta
ID: 7186849
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 7186865
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
 

Author Comment

by:magenta
ID: 7186873
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
 
LVL 92

Expert Comment

by:objects
ID: 7186874
> 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
 

Author Comment

by:magenta
ID: 7186889
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 86

Expert Comment

by:CEHJ
ID: 7186900
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
 

Author Comment

by:magenta
ID: 7186911
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 7186923
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
 

Author Comment

by:magenta
ID: 7186942
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
 

Author Comment

by:magenta
ID: 7186946
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
 
LVL 92

Expert Comment

by:objects
ID: 7186947
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 7186952
>>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
 
LVL 92

Expert Comment

by:objects
ID: 7186961
> You guys gotta admit that that's squirrelly...

No, as mentioned above there are two different JVM's involved.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7186981
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
 

Author Comment

by:magenta
ID: 7187219
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
 
LVL 92

Expert Comment

by:objects
ID: 7187268
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 7187366
>>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
 
LVL 92

Expert Comment

by:objects
ID: 7187391
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 7187405
Well I'm not going to lose sleep over 20 points! Have them all :-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 8952004
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
 

Author Comment

by:magenta
ID: 8953643
I requested that these points get split ages ago...
0
 
LVL 35

Expert Comment

by:girionis
ID: 8955589
 Now you can split them too and you do not have to ask a moderator :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 8960308
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

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 …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 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

22 Experts available now in Live!

Get 1:1 Help Now