B4J Library jPython

This library allows you to use python in B4J (based on jython)
Similar to the jInvokeNashorn library in jScriptEngine topic

Can use a string as a script or a file in Objects/scripts/

Attached is the library - jPython.zip
test b4j project - python testing.zip

Simple examples are included in the source.

The python zip is a tad large so won't upload
Here's a link to it - Removed

Added a new example py script - it shows how to
a, interact with b4j
b, create new controls and add to mainform (either by passing mainform.rootpane or not)
c, add event handling for controls (calls a routine in the b4j app)

(Obviously if your app package is not b4j.example.main - edit accordingly)

jPython(no jython jar).jar is just the library without the jython.jar included

if you use that you need to d/load 'jython-standalone-2.7.0.jar' (google jython) and place in libraries folder.

**** see post 7 ******

****** UPDATE 11/12/15
jPython lib
This needs the full path to the script file

py.InitInvocable("myscript.py") becomes py.InitInvocable("./myscript.py") if the script is in current directory
The library will no longer look in /objects/scripts for the file

Added library source


*** Added jython lib.zip
Requires jython.jar to be added with #AdditionalJars
(currently only works in debug mode - still testing to find reason why)
Ok - sorted it now to work in release
There is a new function - pythonLibraries - point this to the library folder
usually C:/jython2.7.0/lib

Latest version is 1.6
 

Attachments

  • python testing.zip
    18.6 KB · Views: 511
  • python testing 2.zip
    20.4 KB · Views: 490
  • jPython (no jython jar).zip
    2.6 KB · Views: 454
  • jPython lib.zip
    2.7 KB · Views: 515
  • jPython lib source.zip
    885 bytes · Views: 446
  • jython lib.zip
    3.6 KB · Views: 452
  • jythonlibraryV1.6.zip
    3.9 KB · Views: 468
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

thanks for this library.
Tried to use on windows 8.1 (no phyton installed) and java 1.8.0_66 but receive error when running the first example.
Have copied jpython.jar & xml and jython-standalone-2.7.0 (37mb) to the B4J additional libraries folder.

Any hints how to resolve? Is a Phyton installation required (v2 or 3?)

B4X:
...
' ******* using script file in objects/scripts/ *******
    Log("Init Invocable")
    py.InitInvocable("testscript1.py")
    Log("Invoke Test1")
    py.Invoke("test1",Array As String("mary had a little lamb it's fleece was white as snow"))
...

B4X:
Program started.
Init Invocable
main._appstart (java line: 60)
java.lang.NullPointerException
    at jPython.jPython.InitInvocable(jPython.java:34)
    at b4j.example.main._appstart(main.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.start(main.java:36)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
I just checked my setup
Although I have python installed it is not on any path/classpath environment variable.

The only thing I did (which I should have probably mentioned) was to add the standalone jar to the classpath variable.

Still sorting it - looks like it never acquired the script engine.
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Sorry people - I screwed up somewhere - for the moment it will only run in debug

I will get it sorted...
 

Daestrum

Expert
Licensed User
Longtime User
I have it working now in debug and release
Just sorting out instructions for set up
 

Daestrum

Expert
Licensed User
Longtime User
This is how mine is set up

In java folder
c:/......./java/jdk.../jre/lib/ext I have the jython-standalone-2.7.0.jar

In b4j extralibs I have jPython.jar + xml

(This may be where the problem lies)
folder jython 2.7 full - contains the full download of version 2.7 - source code too

In my class path I have c:/jython 2.7 full/lib and c:/jython 2.7 full/javalib

I have an environment variable set to JYTHON_HOME = c:/jython 2.7 full
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hmm ... thx but still not working in release mode (same error as mentioned post #2), although in debug it works.

Had never heard of jython - interesting to explore further to see it it runs on a Raspberry Pi.
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi
It always give me "testscript1.py" not found" when i run it in B4J. The file is in /scripts/ when i unzip example.



Hi,

thanks for this library.
Tried to use on windows 8.1 (no phyton installed) and java 1.8.0_66 but receive error when running the first example.
Have copied jpython.jar & xml and jython-standalone-2.7.0 (37mb) to the B4J additional libraries folder.

Any hints how to resolve? Is a Phyton installation required (v2 or 3?)

B4X:
...
' ******* using script file in objects/scripts/ *******
    Log("Init Invocable")
    py.InitInvocable("testscript1.py")
    Log("Invoke Test1")
    py.Invoke("test1",Array As String("mary had a little lamb it's fleece was white as snow"))
...

B4X:
Program started.
Init Invocable
main._appstart (java line: 60)
java.lang.NullPointerException
    at jPython.jPython.InitInvocable(jPython.java:34)
    at b4j.example.main._appstart(main.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.start(main.java:36)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
 

Attachments

  • jpython.png
    jpython.png
    16 KB · Views: 486

Daestrum

Expert
Licensed User
Longtime User
I think the path inside the library was wrong - just compiling new version
 

inakigarm

Well-Known Member
Licensed User
Longtime User
Hi @Daestrum, hope you can help with this error

I have Windows 8.1, no Python installed and copied jython and jpython jars to Additional libraries folder on B4J 3.71. Trying to test your library with test1 or test 2, program crash with this error

B4X:
Program started.
engines :[jdk.nashorn.api.scripting.NashornScriptEngineFactory@27f023ed, org.python.jsr223.PyScriptEngineFactory@354ee1f1]
failed to acquire script engine
Error occurred on line: 86 (Main)
java.lang.NullPointerException
    at jPython.jPython.Invoke(jPython.java:26)
    at b4j.example.main._appstart(main.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:593)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.start(main.java:36)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/1038866469.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/1030870354.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1864262019.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/410424423.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/186276003.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Any idea about this error ??
 

Daestrum

Expert
Licensed User
Longtime User
@inakigarm Is that in release or debug ?

Added the library source - it's so little yet causes so many problems....

Most of the problems is the classpath I think, finding the required jars.
 
Last edited:

jinyistudio

Well-Known Member
Licensed User
Longtime User
I think the path inside the library was wrong - just compiling new version
Sorry, I don't reply to you, You are right "just compiling new versio" ;)

Wishing you peace, joy and happiness through Christmas and the coming year.
 

Daestrum

Expert
Licensed User
Longtime User
Hi Daestrum

Could i use MinimalModbus with jPython in the B4J !?
If it uses standard python libs, I don't see why not.

@Daestrum , I didn't understand exactly your post; is there anything that I could try to solve this issue?

Thanks
It looks like it cannot find the libs so it cannot find the scriptengine.
(Sorry for the long delay in replying - windows 10 removed all my old windows directories so I have lost all the sources. Glad I posted it on here.)
 

Daestrum

Expert
Licensed User
Longtime User
Added better version of library (still only seems to work in debug mode - still testing)
Use:
B4X:
' in Globals
dim jp as jInvokePython
...
' initialize the script
' if name ends .py assumes a file
' no .py suffix assumes string passed containing script
' starts http - assumes network read of script (experimental)
jp.InitInvocable("c:/temp/pythontest1.py")
...
' call a function in the script
jp.Invoke("test1",Null)
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Got it working properly in release mode now.
Added a new function pythonLibraries - simple pass a string pointing to the jython/lib folder ie
B4X:
jp.pythonLibraries("C:/jython2.7.0/lib")
 
Top