[Windows] All OH logs stop when Jython is in the classpath

For future reference, I just ran into this while trying it out (on Windows), and solved it by doing the following:

  1. Don’t copy jython-standalone-2.7.0.jar to conf\automation\jython, copy it to runtime\lib\boot instead
  2. Instead of adding:
    set EXTRA_JAVA_OPTS=-Xbootclasspath/a:C:\openhab2\conf\automation\jython\jython-standalone-2.7.0.jar -Dpython.home=C:\openhab2\conf\automation\jython -Dpython.path=C:\openhab2\conf\automation\lib\python
    to start.bat, simply add:
    set EXTRA_JAVA_OPTS=-Dpython.home=C:\openhab2\conf\automation\jython -Dpython.path=C:\openhab2\conf\automation\lib\python

Be advised this probably won’t survive an upgrade since the runtime directory gets pretty much overwritten (and you’re not really supposed to touch it much), but for quick testing it does the trick. Hope this helps.

1 Like

Thank you, Yannick! That sounds like a good workaround. You are correct… the update script removes /runtime/ completely, which is why I have not used /runtime/lib/boot/ or /runtime/lib/ext/ in the helper library setup instructions to prevent people having to setup everything again after an update. Well… that and because /runtime/ shouldn’t be messed with. I’m experimenting with a few things to ease the setup of Jython (and Groovy), which will also help out Windows users with this issue. It has just been very slow going using Eclipse for the IDE.

1 Like

Meanwhile, I’m experimenting with the script editor in the UI in the 3 supported languages :slight_smile:
I think it could be an alternative to write your rules in in the personal folder for simple stuff, and still be able to import the library code.

The Python libraries load pretty well but I get ClassNotFoundExceptions when loading most files from the JavaScript libraries. Still, fascinating stuff, being able to use 3 programming languages at will, even in the same rule!


Beautiful! My crystal ball says 50% of users will use straight UI rules, 30% will use Scripted Conditions and Actions (the audience for the features you’ve added… and this number will grow when there is an ExtensionService for templates, etc.), and the remaining 20 will just use scripted automation through files.

BTW, there are not just three supported languages. With openhab-core/#635, all installed script engines will appear in the ‘Script Type’ dropdown for Scripted Conditions and Actions. I have only tested with the three you have been using.

Absolutely! With a Scripting API, the helper libraries will no longer be needed either. In case you haven’t seen this, here is a project that I have working from. This should be recreated in OHC… or even better, move it to a new repo for automation (both old and new rule engines, script engine addons, automation addons, rule templates, a new marketplace extension service, etc.). I’ve been waiting until OH3 got closer to get that rolling.

From the looks of it, you will not be needing the scriptlanguage context.

Please create an issue and I will look into it. I have not had any issues with the JS libraries. They are still evolving and will only truly blossom with JDK9 and ES6.

BTW, the JSON rules may not be affected by this, but you might run into it if you are testing scripted automation on Windows.

You may be right, I suspect the majority of users want to be able to edit simple scripts relatively easily, as long as it’s possible to basically: retrieve item states, send commands, do a little bit of math, string/date/time/array/object/UoM manipulations, logging, and eventually HTTP requests, parse JSON/XML, call actions, set timers… these cover most of the use cases I think.
I also added the ability to edit rules’ tags and already have some ideas to give them some meaning in various parts of the UI.

  • The schedule page will only display rules tagged with “Schedule” (that’s already done actually) so that technical rules running e.g. every minute don’t clutter the timeline;
  • A rule tagged with “Scene”, “LivingRoom” (semantic tag) and “Blue” will lead to a blue button in the card generated for that room on the Locations home page tab to launch that rule/scene.
  • Rules tagged with “Scene”, “Starred” (or similar), and a color (morning routine, going away, welcome home, bed time…) would be displayed prominently on the Overview home page tab as colored buttons.

Well, I had to try and sure enough… :laughing:

I’m especially interested in JRuby since it’s my go-to language for general scripting, just made some tests and it’s encouraging:

require 'java'
require 'net/http'

puts 'Hello from Ruby!'

events.sendCommand('DemoSwitch', 'OFF')
puts ir.getItem('GF_Living')

puts org.joda.time.DateTime.now
puts java.time.LocalDate.now
puts java.lang.System.getenv('OPENHAB_CONF')

uri = URI('http://www.google.com')
res = Net::HTTP.get_response(uri)
puts res.code, res.message

uri = URI('http://localhost:8080/rest/persistence')
puts "API call result: #{Net::HTTP.get(uri)}"

  logger = org.slf4j.LoggerFactory.getLogger('jsr223.jruby')
  puts logger.methods
rescue => e
  puts e

  http = org.eclipse.smarthome.io.net.http.HttpUtil
  puts http.methods
rescue => e
  puts e

results in:

Hello from Ruby!
GF_Living (Type=GroupItem, Members=5, State=NULL, Label=Living Room, Category=video, Tags=[LivingRoom], Groups=[gGF])
08:59:34.309 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'DemoSwitch' received command OFF
API call result: [{"id":"rrd4j","label":"rrd4j","type":"Queryable"}]
missing class name (`org.slf4j.LoggerFactory')
missing class name (`org.eclipse.smarthome.io.net.http.HttpUtil')

I didn’t immediately manage to load slf4j and OH classes, as for the rest it’s working pretty well, including accessing scope objects, and interestingly requests made with the Ruby ‘net/http’ library, so that’s pretty neat. :smiley:
Maybe it’s related to the similar problem I have with Nashorn, I’ll open an issue.

1 Like

I think @rlkoshak may be interested hearing about that one! Where did you get the ScriptEngines from? I’ve been curious to try a recent version of Kotlin.

We should start a new thread! One thing that I’d really like to discuss with you about the OH3 UI is the ability to have multiple paths through a UI rule. I think the functionality might already be in the framework, at least partially, but if it is not, it should get implemented. I expect this would have a huge impact to the rule UI.

Very exciting stuff
Yes please do start a new thread

I’m more interested in Scala or Clojure but IIRC getting JRuby to run in the first place wasn’t the problem. It was building that bridge between Ruby and OH that became a challenge. See Using JRuby for rules for the latest (2017) I’ve seen it discussed.

I made some more attempts since then and got a bit further at least. Seems the scriptExtension and other scope values is global values in Ruby, which means they need to be prepended with a $, so using $scriptExtension.importPreset() works at least. I never got through to create rules though, had problems subclassing SimpleRule, and creating rules using the RuleRegistry was too complicated at the time, but I think it should work. @ysc have you tried any of that?

I noticed this too but that’s the case if you run Ruby 2.3.x i.e. JRuby 9.1.x (downloaded from https://www.jruby.org/download). If you run the latest JRuby 9.2.x which is Ruby 2.5 then you don’t need the dollar signs anymore.

Also I managed to at least get the logging working from a scripted rule action - JRuby seems to use its own ClassLoader, probably to allow things like described in https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#accessing-and-importing-java-classes, you can however get hold of the EquinoxClassLoader from one of the objects in the script scope using reflection, then invoke the getLogger static method like this:

puts ir.java_class.class_loader.inspect
loggerFactoryClass = ir.java_class.class_loader.load_class('org.slf4j.LoggerFactory')
method = loggerFactoryClass.getMethod('getLogger', java.lang.String)
logger = method.invoke(nil, 'jsr223.ruby')
logger.debug('Log from Ruby!')
08:39:22.454 [DEBUG] [jsr223.ruby                          ] - Log from Ruby!       

Pretty ugly and I don’t know if it would work in a .rb in the automation folder but it could probably be streamlined to load any class from OH. Maybe there’s a way to tell JRuby to use the Equinox class loader instead, but I didn’t find it.

Actually Kotlin doesn’t work at all :laughing: It appears to register the script engine since it’s in the list but when I try to execute a script action with some Kotlin code this is what I get:

[ERROR] [ript.internal.ScriptEngineManagerImpl] - ScriptEngine for language 'text/x-kotlin' could not be found for identifier: d4889621-ddea-454a-b33b-5f2030ed4637

I got the .jars from there: https://github.com/JetBrains/kotlin/releases/tag/v1.3.50

1 Like

@5iver and @ysc, when I initially loaded M4 on my Win10 machine (without @ysc 's workaround, but following @5iver 's instructions for start.bat, etc), it broke the logging/jython just as it did back in M2. I then noticed this workaround ([Windows] All OH logs stop when Jython is in the classpath) and decided to give it a try with 2.5.0M4. It does seem to help, but doesn’t get jython fully working. I get logging working again, and everything seems to load/initialize except jython scripts in any folders. If I “touch” a script (take 000_startup_delay.py for example) it will say "Loading script ‘python/core/components/000_startup_delay.py’, but that is it. Nothing else happens. I can go through and try to load each of them, but the rules don’t run. It was working flawlessly in 2.4 stable with setenv.bat file to what it is in 2.5 (per @5iver’s post [Windows] Using EXTRA_JAVA_OPTS, and then adding the required line to my start.bat.)

Any ideas?

This is another bug for Windows, but possibly evolved some… [Windows] All OH logs stop when Jython is in the classpath

Yes, I’m very familiar with that issue, and I was hoping after waiting a few months that maybe it had been solved, but something definitely got better after applying this fix:

I wonder if @ysc saw similar behavior as I did and maybe had to do something else to get it totally working.

Just an update, I forgot to update org.ops4j.pax.logging.cfg with all of the jython logging stuff, so that made some of the logging look more normal, and I can now see that jython is working, however NOTHING gets loaded automatically at startup. None of my jython rules scripts work until I “touch” them (change 1 character and re-save), and none of the core.components scripts auto-load.

I only use openHAB on Windows for dev/test purposes, haven’t tested Jython with M4 yet but when I do I’ll be sure to check that. I guess it’s probably still

or a related issue.


I use M4 on windows10 and I’m struggling to start NGRE Rule engine.
Now I managed to work “Hello world” on Java.
Phyton: any core.x module load give importerror: “ImportError: No module named core in at line number xx”
Groovy: nothing happens

I installed based on https://openhab-scripters.github.io/openhab-helper-libraries/Getting%20Started/Installation.html and i copied jython standalone runtime\lib\boot instead.
Example files are renamed.
But here I can’t find why modules are not loaded.

Are you s sure you copied the Helper Libraries to the right folder? The contents if the Core folder (automation on down) needs to be copied to $OH_CONF. You don’t copy the whole helper library over.

Here is my automation library:

only /core/ folder copied. Path: c:/openhab2/conf/automation it is on windows10.

Here is my ruleengine.log during a startup and I try to load one .py rule after startup:
ruleengine.log (27.0 KB)

Have you restarted OH? People have reported this error when Jython is not installed correctly. Did you modify EXTRA_JAVA_OPTS with the python.home and python.path? You still need these even if you bypass the bootclasspath by putting the Jython jar in /runtime/bin/boot.

This is my startup.bat file:

I have added the same to sh.bat as my Openhab runs as a service.
I can add as an windows enviroment variable instead.
I have tried it before but there was no difference but it was before I have changed the loacation of Jython standalone file. Sorry I have tried many variation and that’s why I ask some help. So I welcome any suggestion as I think I have a tiny bug somwhere. OH was restared after each change. If you suggest I can do it more time.