Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Maarten J
Accessing custom java class from Hook
February 8, 2013 3:20 PM
Answer

Maarten J

Rank: New Member

Posts: 18

Join Date: January 25, 2012

Recent Posts

I've been spending several hours this evening finding a solution, but I was not able to solve the issue I had.

The first thing which I did today was creating a hook. This is working fine. In this hook I want to access a java file which I have written myself. This java file makes use of org.apache.commons.httpclient.HttpClient.

As far as I understand, I can not add this java file in my hook as it won't be in the correct classpath. Instead I have to create an Ext Plugin. I've tried that, but it still not working. The Ext Plugin has the following content:

A java file in docroot/WEB-INF/ext-impl/src with package name rcs.ext.utilities. As mentioned before, this java file is importing org.apache.commons.httpclient.HttpClient. The problem I had with this is that Liferay IDE didn't detect the Portal Dependency set in liferay-plugin-package.properties. I've solved this by manually adding a jar in WEB-INF/ext-lib/portal. I then added this jar file to the build path. The extension then deployed without any issues.

Bug: Liferay IDE does not detect the dependency jars for an Ext Plugin (it did for a hook).

After deploying the Ext Plugin I tried to access the just added java file in my jsp hook by doing:

<%@ page import="rcs.ext.utilities.MyNewJavaClass;" %> (with and without ;)

this didn't work.

To sum up:
  1. I think I've found a bug in Liferay IDE not detecting dependency jars which causes java files to show errors (instead you have to manually import jars and add them to the build path)
  2. I think I used the right approach by creating a hook and an ext plugin (to add the java file) but I'm not sure. Is it?
  3. I'm not able to access the java file in the Ext Plugin from the JSP hook. What am I doing wrong?


Any help on this would be great!

Thanks
David H Nebinger
RE: Accessing custom java class from Hook
February 8, 2013 6:20 PM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11492

Join Date: September 1, 2006

Recent Posts

Maarten J:
A java file in docroot/WEB-INF/ext-impl/src with package name rcs.ext.utilities. As mentioned before, this java file is importing org.apache.commons.httpclient.HttpClient. The problem I had with this is that Liferay IDE didn't detect the Portal Dependency set in liferay-plugin-package.properties. I've solved this by manually adding a jar in WEB-INF/ext-lib/portal.

Bug: Liferay IDE does not detect the dependency jars for an Ext Plugin (it did for a hook).


Exts are oddballs. Code that you put in here needs the jars for compile time only, but not for deployment. liferay-plugin-package.properties dependencies makes no sense because the code is, in the end, deployed to ROOT/WEB-INF/lib and therefore has visibility on everything that's there. You just need to add the jar(s) to the build path in Eclipse (ant build actually should work just fine w/o the addition).

After deploying the Ext Plugin I tried to access the just added java file in my jsp hook by doing:

<%@ page import="rcs.ext.utilities.MyNewJavaClass;" %> (with and without ;)


JSP hooks are applied within the ROOT webapp after deployment. Errors that you see in Eclipse do not matter, as it's the runtime deployment context that matters.

To sum up:
  1. I think I've found a bug in Liferay IDE not detecting dependency jars which causes java files to show errors (instead you have to manually import jars and add them to the build path)
  2. I think I used the right approach by creating a hook and an ext plugin (to add the java file) but I'm not sure. Is it?
  3. I'm not able to access the java file in the Ext Plugin from the JSP hook. What am I doing wrong?


1. I explained this one, it's not really a bug as much as it is the nature of the ecosystem.
2. That works. Your only other option (to have code visible in a hook that is not in LR) would be to put the jar in the global class path, but that has issues also (you must shut down the container to deploy an update).
3. It probably will work, but errors will show within Eclipse.
Maarten J
RE: Accessing custom java class from Hook
February 9, 2013 3:04 AM
Answer

Maarten J

Rank: New Member

Posts: 18

Join Date: January 25, 2012

Recent Posts

David, thanks for the clear explanation. As far as I can see I was doing most things right. Everything is deploying fine. The thing is that when I access the page page which contains the hook where I make use of the new class the log shows the following:

 1
 2An error occurred at line: 215 in the generated java file
 3Only a type can be imported. rcs.ext.utilities.TinyUrlUtil resolves to a package
 4
 5Stacktrace:
 611:01:26,915 ERROR [IncludeTag:154] org.apache.jasper.JasperException: Unable to compile class for JSP:
 7
 8An error occurred at line: 215 in the generated java file
 9Only a type can be imported. rcs.ext.utilities.TinyUrlUtil resolves to a package
10
11Stacktrace:
12    at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
13    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
14    at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
15    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
16    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
17    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
18    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
19    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)


I'm importing the file using

1
2<%@ page import="rcs.ext.utilities.TinyUrlUtil;" %>
Maarten J
RE: Accessing custom java class from Hook
February 11, 2013 12:19 AM
Answer

Maarten J

Rank: New Member

Posts: 18

Join Date: January 25, 2012

Recent Posts

I was able to find the problem, the Liferay IDE was not deploying the Extension in the right way. I copied the war of the extension to the deploy folder and then it worked.