5 Replies Latest reply on May 18, 2006 8:12 AM by billwithaheadache

    Java w/ bultin in MX7 package?

      Is it possible to use Java libraries already installed with MX7, from a custom .class file?

      Say with something like the attached code... I compile it with iText.jar from the MX7 install \lib dir in the classpath, and copy it to the WEB-INF\classes dir. Calling the getNameOf() method just gives me some useless error:

      500 com/lowagie/text/pdf/PdfName

      With more complex files I get a NoClassDefFoundError exception for the iText classes.

      Any ideas?
        • 1. Re: Java w/ bultin in MX7 package?
          James74 Level 1
          Not sure about the mechanics , but I have used the Java Libraries in a custom tag and right in Coldfusion itself (know enough to get pieces of code working, but that's about it). Here's an example that I found on the Web for calling a Java Library already installed with CFMX:


          s = "";
          cls = s.getClass();

          stringClass = cls.forName("java.lang.String");

          fileReaderClass = cls.forName("java.io.FileReader");

          a = arrayNew(1);
          a[1] = stringClass;

          fileReaderConst = fileReaderClass.getConstructor(a);

          a[1] = form.upFile;

          fileReader = fileReaderConst.newInstance(a);

          readerClass = cls.forName("java.io.Reader");
          a[1] = readerClass;

          bufferedReaderClass = cls.forName("java.io.BufferedReader");
          bufferedReaderConst = bufferedReaderClass.getConstructor(a);

          a[1] = fileReader;
          bufferedReader = bufferedReaderConst.newInstance(a);

          try {
          do {
          s = bufferedReader.readLine();
          } while (true);
          } catch (coldfusion.runtime.UndefinedVariableException e) {
          // this indicates end of file, ok to ignore error

          Hope this helps

          • 2. Re: Java w/ bultin in MX7 package?
            jboschen Level 1
            I'm doing something similar to that now, but it's a pain to debug and just about as bad to code.
            • 3. Re: Java w/ bultin in MX7 package?
              ksmith Level 1
              There are many ways to do it. CFMX7 enterprise server allows you to access the J2EE features of JRun. So, you can write JSPs, servlets, etc. and call them directly or through CFML. The developer's guide has a brief chapter on java and cfmx. You can also code java classes and then call them with cfobject/java in any version of CFMX. You can also use java tag libraries and call them.

              There are articles on java integration on the Macromedia/Adobe website in the developers center. Also, just googling on "java coldfusion" brings up many pages of articles. Finally, I know there was a series of articles on the subject in ColdFusion Developers Journal.
              • 4. Re: Java w/ bultin in MX7 package?
                jboschen Level 1
                Yes, I know all this. My specific question is how to access packages installed by MX7, such as iText, from a Java .class file that I write and install. I have no problem with how to load a custom .class using CFOBJECT, or how to access the libraries from CFML/CFSCRIPT. The problem is that I can't use the version of iText installed with MX7 from Java in a custom .class file.
                • 5. Re: Java w/ bultin in MX7 package?
                  We're running into similar problems with existing custom java classes as we move to MX7. *It may be a bug*. It _appears_ that the MX7 java class loader does not behave as expected - it certainly behaves differently than in MX6. To reproduce the 500 java/package/name error, try placing your custom java classes in the WEB-INF/classes directory. This also occurs when you place your jar file in the WEB-INF/lib directory. If you look at the exception log, you will see a java.lang.NoClassDefFoundError, which tells me that the class was not correctly or fully loaded. The class loader appears to load the class - no ObjectInstantiationException - yet throws the NoClassDefFoundError exception when invoking _some_ methods. The problem is difficult to isolate because _some_ class methods work while others do not, throwing the NoClassDefFoundError.

                  The workaround I have found so far is to jar up the classes and place it in the CFusionMX7/lib directory and restart the server. This is how we have our production servers configured anyway. This is an obvious problem for development as the CFusionMX7/lib is not a dynamic class loading directory and any changes made to classes therein require a server restart to be effectuated.

                  In short, and I plan to start another thread, is that the /WEB-INF/classes and WEB-INF/lib directories do not load classes as expected in CFMX 7.

                  Adobe, please correct me if I am missing something.