[go: up one dir, main page]

Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Advanced Scripting Environment (EASE) » Load Java Class in Script from current workspace
Load Java Class in Script from current workspace [message #1731342] Wed, 04 May 2016 12:55 Go to next message
Daniel K. is currently offline Daniel K.Friend
Messages: 11
Registered: May 2012
Junior Member
Hi,
I have a general question:
In the topic https://www.eclipse.org/forums/index.php/t/842327/ it is mentioned to be able to load Java classes from the current workspace in a script which is located in the same. The mentioned Gerrit patch seems to be merged.
I have currently installed the newest nightly build from ease, the 0.4.0...
Is this possible to load a Java class in a script (JS for example) and execute it in the same workspace? If yes, what are the steps to achieve this?

Regards
Daniel
Re: Load Java Class in Script from current workspace [message #1731379 is a reply to message #1731342] Wed, 04 May 2016 17:41 Go to previous messageGo to next message
Christian Pontesegger is currently offline Christian PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior Member
Hi Daniel,

have a look at the JVM module:
loadModule("/System/JVM");
	org.eclipse.ease.modules.java.JVMModule@7317ea31
createInstance("workspace://Java 1/src/org/test/Hello.java")
	org.test.Hello@15b6685f


It provides methods to create an instance from a default constructor, to get a class definition or to invoke a static method on a class (eg: main)

Edit: even if we have a method called 'compile' we do not really start a compile process, but use the automated build of eclipse. so we depend that there exists a binary folder and the according class files.

HTH
Christian

[Updated on: Wed, 04 May 2016 17:43]

Report message to a moderator

Re: Load Java Class in Script from current workspace [message #1731722 is a reply to message #1731379] Mon, 09 May 2016 14:29 Go to previous messageGo to next message
Daniel K. is currently offline Daniel K.Friend
Messages: 11
Registered: May 2012
Junior Member
Hi,
thanks for your reply.
Works as a charm.
One question in advance: Is it also possible to load classes with a special constructor? Or is this too difficult? I'm not so deep in the topic.

Best Regards
Daniel
Re: Load Java Class in Script from current workspace [message #1731746 is a reply to message #1731722] Mon, 09 May 2016 18:34 Go to previous messageGo to next message
Christian Pontesegger is currently offline Christian PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior Member
Quote:
Is it also possible to load classes with a special constructor?


Not directly anymore with a single script command. But you could use the compile() method to get the class representation and then use reflection to call your dedicated constructor:
https://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html
Re: Load Java Class in Script from current workspace [message #1731778 is a reply to message #1731746] Tue, 10 May 2016 07:00 Go to previous messageGo to next message
Daniel K. is currently offline Daniel K.Friend
Messages: 11
Registered: May 2012
Junior Member
Hi,
ok, thanks for the fast reply.

Regards
Daniel
Re: Load Java Class in Script from current workspace [message #1735081 is a reply to message #1731778] Wed, 15 June 2016 11:57 Go to previous messageGo to next message
Zakir Meer is currently offline Zakir MeerFriend
Messages: 50
Registered: February 2016
Member
Hi Experts,

In the EASE script file, I am trying to create object of a java class - which internally is using the API's from 3rd party library (jar file added in the classpath of the project) , but all the time I get the java.lang.NoClassDefFoundError (for the class's which are part of jar file and are referred in my java class)

Example:

loadModule("/System/JVM");
createInstance("workspace://p1_prj/src/org/test/Hello.java")


In this case Hello.java (present in plugin project "p1_prj") is making use of PlantUML library API's (jar file added in the class path of "p1_prj" project) .
In the script file, I want to create object of Hello.java and for this I am making use of the API :
createInstance("workspace://p1_prj/src/org/test/Hello.java")

When script file is executed, following error is thrown :
java.lang.NoClassDefFoundError: net/sourceforge/plantuml/SourceStringReader

- By looking at the error, it is clear for me that the class's which are part of the 3rd party library (jar) are not loaded during the execution of EASE script file.

Can you please let me know if there is a mechanism to implicitly load the class's of 3rd party libraries (added as jars in the classpath) when object for a java class is to be created

Thanks a lot !!
Re: Load Java Class in Script from current workspace [message #1735134 is a reply to message #1735081] Wed, 15 June 2016 17:52 Go to previous messageGo to next message
Christian Pontesegger is currently offline Christian PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior Member
Just did a quick check on the code and it seems we only consider project dependencies in the classpath. So as a workaround you could create a plugin from your jar in your workspace. To really consider external jars we need to change the classloading mechanism. Please raise a bug for that.

Christian
Re: Load Java Class in Script from current workspace [message #1735196 is a reply to message #1735134] Thu, 16 June 2016 09:23 Go to previous messageGo to next message
Zakir Meer is currently offline Zakir MeerFriend
Messages: 50
Registered: February 2016
Member
Hi Christian,

Thanks for your quick reply !!

As you have mentioned, I have reported a bug (https://bugs.eclipse.org/bugs/show_bug.cgi?id=496245) regarding loading of class's from jar files (which are part of the classpath)

** Regarding the workaround which you have mentioned: to convert jar file as a eclipse plugin and add it as a plugin dependency

I tried this approach, but this time I get other kind of error from JVM module.

To reproduce this issue, I have attached a zip file containing : TestProject and PlantUML Plugin (converted from jar)

Find the detailed stack trace below:

org.eclipse.ease.ScriptExecutionException: JavaError: null
	at workflow.js

Java Stacktrace:
java.lang.NullPointerException
	at org.eclipse.ease.lang.jvm.compiled.JVMCompiledScriptEngine.loadClass(JVMCompiledScriptEngine.java:251)
	at org.eclipse.ease.modules.java.JVMModule.compile(JVMModule.java:96)
	at org.eclipse.ease.modules.java.JVMModule.createInstance(JVMModule.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
	at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
	at org.mozilla.javascript.gen.Wrapper_JVMModule__66._c_createInstance_3(Unknown Source)
	at org.mozilla.javascript.gen.Wrapper_JVMModule__66.call(Unknown Source)
	at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:63)
	at org.mozilla.javascript.gen.workflow_js_63._c_script_0(Unknown Source)
	at org.mozilla.javascript.gen.workflow_js_63.call(Unknown Source)
	at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
	at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
	at org.mozilla.javascript.gen.workflow_js_63.call(Unknown Source)
	at org.mozilla.javascript.gen.workflow_js_63.exec(Unknown Source)
	at org.mozilla.javascript.Context.evaluateReader(Context.java:1110)
	at org.eclipse.ease.lang.javascript.rhino.RhinoScriptEngine.internalExecute(RhinoScriptEngine.java:182)
	at org.eclipse.ease.lang.javascript.rhino.RhinoScriptEngine.execute(RhinoScriptEngine.java:163)
	at org.eclipse.ease.AbstractScriptEngine.inject(AbstractScriptEngine.java:182)
	at org.eclipse.ease.AbstractScriptEngine.run(AbstractScriptEngine.java:236)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)








Re: Load Java Class in Script from current workspace [message #1736519 is a reply to message #1735196] Thu, 30 June 2016 07:09 Go to previous messageGo to next message
Zakir Meer is currently offline Zakir MeerFriend
Messages: 50
Registered: February 2016
Member
Hi Christian,

any hints regarding my above post?
Re: Load Java Class in Script from current workspace [message #1736632 is a reply to message #1736519] Thu, 30 June 2016 16:41 Go to previous messageGo to next message
Christian Pontesegger is currently offline Christian PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior Member
You have to wait for the bugfix, sorry. I am currently quite busy with some other topics, so a solution will be delayed a bit.
Re: Load Java Class in Script from current workspace [message #1737243 is a reply to message #1736632] Wed, 06 July 2016 14:32 Go to previous messageGo to next message
Christian Pontesegger is currently offline Christian PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior Member
I have pushed a fix for this today. As we are using URLs to resolve external paths this needs more checking on different operating systems, special characters in the path, ...
Please report back if the problem persists.

The fix will be online with the next nightly build.
Re: Load Java Class in Script from current workspace [message #1737442 is a reply to message #1737243] Fri, 08 July 2016 09:46 Go to previous messageGo to next message
Zakir Meer is currently offline Zakir MeerFriend
Messages: 50
Registered: February 2016
Member
Hi Christian,
It looks very good so far.
with the nightly build from 07.July( http://download.eclipse.org/ease/update/nightly ) + windows 7 OS : In the EASE scripts, loading of class's from the libraries (jars in the class path) works perfectly fine.
Thanks a lot for the implementation.
As a reference please find the attached zip files containing sample projects which I have used to test this implementation (tests are green Smile)

Do you plan to add this as a bugfix to the version 0.3.0 ? or are you treating this as a new feature ?
Re: Load Java Class in Script from current workspace [message #1737501 is a reply to message #1737442] Fri, 08 July 2016 19:05 Go to previous message
Christian Pontesegger is currently offline Christian PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior Member
Thanks for reporting back. This fix will be part of the next release (0.4.0) - and before you ask, we do not have a release date for this Smile Very likely before EclispeCon Europe.
Previous Topic:Dedicated Start/End Actions for Script
Next Topic:Support for Bean Shell
Goto Forum:
  


Current Time: Thu Dec 12 16:47:04 GMT 2024

Powered by FUDForum. Page generated in 0.08391 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top