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

Tags: #<Tag:0x00007f616df5c910>

Wow, you know I think I vaguely remember reading that years ago when I first loaded openhab, but after numerous “sudo apt update/upgrade” I guess somehow I ended up with 11. I’ll try to figure out how to revert to 8.

Thanks for the heads up.

This will definitely cause issues. Which Linux distribution are you using?

Are you using the update script? I don’t use a repository installation, but I would think you’d need to upgrade an existing installation rather than installing again over the top of another version. If downgrading the Java does not help, I suggest backing up, uninstalling, and then reinstalling from scratch.

I am using Ubuntu Server 18.04.3 LTS on the Linux box.

No, I am not using the update script, I don’t think I communicated my method very well… I actually use the repository and did a Package Repository Installation to originally install, and then I use the procedure here to upgrade or downgrade and choose the specific build I want, as I’ve done a few times in the last day or two.

However, the main update is that I rolled back from Java 11 to Java 8 using this guide and I’m now in business on JSR233/Jython! It wasn’t exactly seamless when I fired up OpenHAB after switching to Java 8, it took about 3 restarts of OH, and streams and streams of red error logs, but eventually it settled out. And, the RestDocs UI now works as well. There are some things that have stopped working that I need to figure out (seems like executeCommandLine and sendHttpGetRequest might be broken) but I will have to troubleshoot tomorrow.

Thanks for the help thus far though!

2 Likes

Correction to my post above, the RestDocs UI is still broken when GoogleTTS is installed (can’t find swagger.json). And, sound commands like “playSound()” and “say()” are not working, from rules DSL or from Karaf.

However, pretty much everything else seems to be working, false alarm on “executeCommandLine()” and “sendHttpGetRequest()”.

The problem with RestDocs is related to…


These are working for me… and work in Jython too.

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!

2 Likes

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)}"

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

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

results in:

Hello from Ruby!
GF_Living (Type=GroupItem, Members=5, State=NULL, Label=Living Room, Category=video, Tags=[LivingRoom], Groups=[gGF])
2019-10-04T08:59:34.308+02:00
2019-10-04
/home/ys/oh/conf
08:59:34.309 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'DemoSwitch' received command OFF
200
OK
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!')
#<Java::OrgEclipseOsgiInternalLoader::EquinoxClassLoader:0x68f8e05c>                                                  
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.