Lucee 7 java import statement

I’ve been tinkering with lucee 7 (L7). I’m running an experimental version of the lucee app that powers our CRM. I love how snappy everything feels. I came across the Java Class Interaction link documentation at:
JAVA CLASS INTERACTION

I love the idea of being able to import java classes just using import. Currently in our app I use the following:

        var jarpath = expandPath("./assets/pdfbox/")
        var jars = [
            jarpath & "pdfbox-3.0.5.jar",
            jarpath & "fontbox-3.0.5.jar",
            jarpath & "pdfbox-io-3.0.5.jar",
            jarpath & "commons-logging-1.2.jar"
        ]
var loader = createObject("java", "org.apache.pdfbox.Loader", jars)

I’m dynamically loading the jar files for the latest version(at least back when I created this) of pdfBox.

On experimenting with L7’s implementation; loading the pdfbox jar I used:

import org.apache.pdfbox.Loader;

On refresh of the page, this generates no errors. My assumption is that it’s loading whatever version of pdfbox that lucee ships with. Not the version that I have stored in my assets directory.

With the import statement, is there a way to direct it to my assets folder?

To use import here use the new java settings syntax

yaml = new component javasettings='{"maven":["org.yaml:snakeyaml:2.4"]}' {
    import org.yaml.snakeyaml.*;

    function read(filePath) {
        var yaml = new Yaml();
        return yaml.load(fileRead(arguments.filePath));
    }
};

// Usage
customer = yaml.read("customer.yaml");
dump(customer); // Shows the parsed data as a map
1 Like

in your example you can simply do

PDFBoxFactory.cfc

// maven source https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
component   javaSettings='{
		"maven": [
			"org.apache.pdfbox:pdfbox:3.0.6"
		]
	}'  {

	import "org.apache.pdfbox.Loader";
	import "java.io.File";

	static function loadPDF(required string pdfPath) {
		return Loader::loadPDF(new File(pdfPath));
	}
}

index.cfm

dump(PDFBoxFactory::loadPDF("/path/to/my.pdf"));

took me less than 5 minutes to write, including looking up the maven endpoint for pdfbox.
BTW you also can define javasettings in .CFConfig.json to have it avialble globally lucee-docs/docs/recipes/java-settings.md at master · lucee/lucee-docs · GitHub