How to get started with Lucee integration with Java?

Someone “threw” the following java code to me a few days ago, thought, it might be a good way for me to get started with some light Java integration with Lucee (Railo probably would struggle for that… fyi, I’m not a Java programmer):

	private struct function httpRequest(url) {
		var httpClient = createObject("java","org.apache.http.impl.client.DefaultHttpClient");
		var httpGet = createObject("java","org.apache.http.client.methods.HttpGet");
		var jURL = createObject("java", "").init(arguments.url);
		var host = jURL.getHost();
		var path = jURL.getPath();
		var httpHostTarget = createObject("java","org.apache.http.HttpHost").init(host,80,"https");
		var localContext = createObject("java","org.apache.http.protocol.BasicHttpContext");
		var httpContent = {};
			httpContent['fileContent'] = '';
			httpContent['statusCode'] = 200;

		var EntityUtils = createObject("java","org.apache.http.util.EntityUtils");


		var response = httpClient.execute(httpHostTarget, httpget, localContext);

		httpContent['fileContent'] = createObject("java","org.apache.http.util.EntityUtils").toString(response.getEntity());

		httpContent['statusCode'] = response.getStatusLine().getStatusCode();


		return httpContent;
  var foo = httpRequest('http/to/url').fileContent;
  var result = httpRequest(url).fileContent;

The above java code does not have any Class, can we compile it into a Java class without class reference? Can Lucee call / reference a .java file directly?

I’ve compiled a tiny hello world Java code into a Java class file, and Lucee is able to find it and execute it. But this is a different case, any guidance would be much appreciated.

Lucee is a Runtime Compiler, means every cfml template is on the fly compiled to java bytecode and loaded.
Mean your code above is producing java classes in the cfclasses folder.
But i don’t think that is what you are looking for.

CFML is perfect to integrate CFML code with Java Code. i did myself years ago apache struts project with CFML as template language, in my opion still the best framework for CFML. Sadly this approach is not widely adopted by the community, most code is still pure CFML based. So i’m all for more java in CFML.

In my opinion you have to do it right. Java is a static language, so if you use Java in your application, it should be compiled and in a jar file. Loose java classes are a no go!
Just my 2c on this.

Bur why exactly you wanna compile java source code on the server?

1 Like


Thank you for your informative and thoughtful response. I’m not a Java programmer nor it’s my plan to become one but a thought is, for a large entity, they would have many systems developed with multiple languages including Java, .NET, php and possibly cfml etc. as well… then, some program manager feels certain system badly needs enhancements which would need new development and leverage some existing elements written in Java, thus, an ability to read Java code would be helpful, and of course API is another option for such integration. In the meantime, having two options would seem more desirable.


(Don) Chunshen Li