Trouble importing custom Java package

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

ericb
Ebase User
Posts: 82
Joined: Fri Jan 15, 2016 2:34 pm

Trouble importing custom Java package

#1

Postby ericb » Fri Feb 26, 2016 4:55 pm

I've read all the posts I could find on the forum about importing Java packages but I haven't been able to come up with a solution to this yet.

I've created a pretty simple Java package with a single class, that has a single method.

Code: Select all

package uohi.MailboxScanner;

import java.util.ArrayList;
import java.util.Properties;
import javax.mail.*;

class Scanner {
	public int foo = 0;
	
	public Scanner() {
		
	}
	
	public ArrayList<String> scanMailbox&#40;&#41; throws Exception &#123;
		// code here
	&#125;
&#125;
I exported the package into a .jar, which is in the WEB-INF/lib folder (and restarted the integrated server). I then do the following in an ebase script:

Code: Select all

importPackage&#40;Packages.uohi.MailboxScanner&#41;;

var scanner = new Scanner&#40;&#41;;
var results = scanner.scanMailbox&#40;&#41;;
When the script is run, I get the following error:

Code: Select all

com.ebasetech.ufs.runtime.events.rhinojavascript.JavascriptExecutionHandler$RhinoScriptException&#58; TypeError&#58; Cannot find function scanMailbox in object uohi.MailboxScanner.Scanner@333d41bf. &#40;checkMailbox#6&#41;
You may have also noticed the "public int foo = 0" in my class definition. I added that just for more testing. If I try to access that in my Ebase script, I get this:

Code: Select all

java.lang.IllegalAccessException&#58; Class org.mozilla.javascript.JavaMembers can not access a member of class uohi.MailboxScanner.Scanner with modifiers "public"
If I remove the "public" in the property definition, there is no error but it just returns "undefined".

What am I doing wrong or forgetting here?
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Mon Feb 29, 2016 9:28 am

The class needs to be public i.e.

Code: Select all

public class Scanner &#123; 
  ..
Also, the convention is that package names are always lower case, though it shouldn't cause any problems if they aren't.
0 x

ericb
Ebase User
Posts: 82
Joined: Fri Jan 15, 2016 2:34 pm

#3

Postby ericb » Mon Feb 29, 2016 2:41 pm

That's all I was missing, thanks.
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#4

Postby Segi » Mon Feb 29, 2016 10:39 pm

ericb,

How did you get this to work ?

I also want to create my own Java class but I can't create an instance of my Java class on the server side.

I tried compiled your code into a Java class, added it to a jar file using jar cvf Scanner.jar Scanner.class then copied the jar into UfsServer\tomcat\webapps\ufs\WEB-INF\lib and restarted the server but I can't create an object. I get the error com.ebasetech.ufs.runtime.events.rhinojavascript.JavascriptExecutionHandler$RhinoScriptException: ReferenceError: "Scanner" is not defined. (TEST_BP#5)

The server side script looks like this:

Code: Select all

importPackage&#40;com.ebasetech.xi.api&#41;;
importPackage&#40;com.ebasetech.xi.services&#41;;
importPackage&#40;Packages.uohi.MailboxScanner&#41;;

var s=new Scanner&#40;&#41;;
The java class looks like this:

Code: Select all

package uohi.MailboxScanner; 

import java.util.ArrayList; 
import java.util.Properties; 

public class Scanner &#123; 
   public int foo = 0; 
    
   public Scanner&#40;&#41; &#123; 
       
   &#125; 
    
   public void scanMailbox&#40;&#41; throws Exception &#123; 
      // code here 
      return;
   &#125; 
&#125;
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#5

Postby Jon » Tue Mar 01, 2016 9:00 am

Possibly the directory structure of the jar file is not correct, it must be uohi/MailboxScanner i.e. the same as the package name. You can open the jar file with a zip program to check this.
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#6

Postby Segi » Tue Mar 01, 2016 11:16 pm

Jon,

Thanks,

That was the issue.

I am developing my own custom package and have to continuously compile the Java class and copy it over to my production server so I wrote a batch file that will compile the Java class, parse the Java source file for the package name, create the directory structure for the jar,create the jar and optionally copy it to the lib folder.

Before running it change JDKPATH to the full path where javac.exe is located and set UFSLIB to the location of webapp\WEB-INF\lib if you want the batch file to automatically copy the jar to the lib folder or leave it to SET UFSLIB= if you don't want to automatically copy it.

It needs to be run in the same location as the java source file. Hope someone can find it helpful.

http://pastebin.com/WVvdSKWn
0 x

ericb
Ebase User
Posts: 82
Joined: Fri Jan 15, 2016 2:34 pm

#7

Postby ericb » Fri Apr 01, 2016 5:34 pm

Bringing this back from the dead a bit.

I'm having more trouble with my .jar file not being recognized properly.

The java class in question is used in a scheduled task. When the task runs on the integrated server, all is well. However, when it tries to run on a standalone server, I get this error.

Code: Select all

ERROR ReferenceError&#58; "Scanner" is not defined. &#40;checkMailbox#11&#41;
com.ebasetech.ufs.runtime.events.rhinojavascript.JavascriptExecutionHandler$RhinoScriptException&#58; ReferenceError&#58; "Scanner" is not defined. &#40;checkMailbox#11&#41;
The jar file is in tomcat\webapps\ebase\WEB-INF\lib. I have other .jar files that were added by me in the same directory for single sign-on, and those work as well.

What could lead to a java class working on the integrated server, but not on the standalone server?
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#8

Postby Segi » Fri Apr 01, 2016 5:36 pm

Couple of things


1. Did you deploy the lib to the target server and restart Tomcat ?
2. Are you using the fully qualified name in the script like com.mypackage.scanner ?
0 x

ericb
Ebase User
Posts: 82
Joined: Fri Jan 15, 2016 2:34 pm

#9

Postby ericb » Fri Apr 01, 2016 5:40 pm

1. The jar file for the class was copied to the UfsServer\tomcat\webapps\ebase\WEB-INF\lib folder. I'm assuming that's all you mean by "deploy"?

2. The class is used like this in the script:

Code: Select all

importPackage&#40;com.ebasetech.xi.api&#41;;
importPackage&#40;com.ebasetech.xi.services&#41;;
importPackage&#40;Packages.uohi.mailboxScanner&#41;;

&#91;...&#93;

var scanner = new Scanner&#40;&#41;;
Like I said, this script runs fine from the integrated server, but not on the standalone.
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#10

Postby Segi » Fri Apr 01, 2016 5:43 pm

I was asking if you deployed the lib on the standalone server but I'm going to assume that you did.

What happens if you use the fully qualified name so instead of

Code: Select all

var scanner = new Scanner&#40;&#41;;
you do this

Code: Select all

var scanner = new Packages.uohi.mailboxScanner.Scanner&#40;&#41;;
0 x

ericb
Ebase User
Posts: 82
Joined: Fri Jan 15, 2016 2:34 pm

#11

Postby ericb » Fri Apr 01, 2016 5:59 pm

What exactly are you referring to when you say "deploy the lib"? If it's anything beyond just copying the .jar file, I might not have done it.

Here's what I get when I try the full name:

Code: Select all

ERROR TypeError&#58; &#91;JavaPackage uohi.mailboxScanner.Scanner&#93; is not a function, it is object. 
com.ebasetech.ufs.runtime.events.rhinojavascript.JavascriptExecutionHandler$RhinoScriptException&#58; TypeError&#58; &#91;JavaPackage uohi.mailboxScanner.Scanner&#93; is not a function, it is object. 
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#12

Postby Segi » Fri Apr 01, 2016 6:25 pm

when I say deploy the lib I mean copy the jar file to the correct lib folder which is webapps\WEB-INF\lib then restart Ebase.

Again, I'm assuming that you did that. I also had that error message occur when building a custom JAR because the JAR was not built correctly using the correct directory structure.

So if the package is uohi.mailboxScanner.Scanner, when you extract the jar using a zip tool like winZip/WinRAR or 7-Zip, it will have the following folder structure:

uohi\mailboxScanner\Scanner\Scanner.jar

If you're using Windows, check out my batch file http://pastebin.com/hd650xAf which automatically compiles the java class into a jar and can optionally copy it to the right location.

You need to edit the file and edit the variable JDKPath if its different on your system. If you set UFSLIB, the jar will be automatically copied to the path you specify in UFSLIB.
0 x

ericb
Ebase User
Posts: 82
Joined: Fri Jan 15, 2016 2:34 pm

#13

Postby ericb » Fri Apr 01, 2016 6:39 pm

I checked the folder structure of my jar and it turns out it was wrong as well. I don't know how it was working on the integrated server, but it was. I rebuilt it in Eclipse and now it looks like it works on both integrated and external servers.

Thanks for the help.
0 x


Who is online

Users browsing this forum: Bing [Bot] and 19 guests