ColdFusion:关于使用定制的“自己写的";Java 类

我需要在 cfml 页面中使用我自己的 java 类.

I need to use my own java class in a cfml page.

文档中的这个条目听起来不错,但是没有解释我必须创建哪些文件.

This entry in the documentation sounds great but does not explain which files I have to create.

我试图在我的网站根目录下创建一个 test.cfm 页面.然后将 TestClass.java + TestClass.class 放在同一路径下.但这会导致错误找不到类"!.​​

I tried to create a test.cfm page under my website root. Then placed TestClass.java + TestClass.class in the same path. But that results in an error "class not found"!.

你能帮帮我吗?

推荐答案

TestClass.java + TestClass.class 在同一路径中.

a TestClass.java + TestClass.class in the same path.

您不能只将 .class 文件放在任何地方.当 CF 服务器启动时,它仅检查类/jar 的特定位置.这些位置称为CF 类路径".你编译的.class文件必须放在CF类路径中,否则检测不到.

You cannot just place .class files anywhere. When the CF server starts, it only checks specific locations for classes/jars. Those locations are referred to as the "CF class path". Your compiled .class file must be placed within the CF class path, or it will not be detected.

使用自定义 java 类:

To use a custom java class:

  1. 创建一个源文件即YourTestClass.java
  2. 将源代码编译成类文件即YourTestClass.class
  3. 将编译好的.class文件放在CF类路径的某处,如:

  1. Create a source file ie YourTestClass.java
  2. Compile the source code into a class file ie YourTestClass.class
  3. Place the compiled .class file somewhere within the CF classpath, such as:

  • WEB-INFclasses - 用于单个 .class 文件
  • WEB-INFlib - 用于 .jar 文件(多个类)

  • WEB-INFclasses - for individual .class files
  • WEB-INFlib - for .jar files (multiple classes)

注意:您还可以通过 ColdFusion 管理员.但是,将类放在默认目录之一更简单.

Note: You could also add the item to the CF class path via the ColdFusion Administrator. However, placing the class in one of the default directories is simpler.

重启 ColdFusion 服务器以检测新类

Restart the ColdFusion server so it detects the new classes

注意:虽然您可以使用单独的 .class 文件,但更常见的是将它们打包成 .jar 文件.

Note: Though you can use individual .class files, it is more common to package them into .jar files.

相关文章