jruby / image_voodoo

ImageScience-compatible image processing for JRuby
MIT License
26 stars 10 forks source link

Launching empty Java app after console starts #16

Closed PetrKaleta closed 9 years ago

PetrKaleta commented 9 years ago

I have this simple rake task:

task :console do
  require 'image_voodoo'
  require 'irb'
  require 'irb/completion'

  ARGV.clear && ARGV.concat(['--prompt', 'simple'])
  IRB.start
end

Once I ran this task, it automatically runs Java application with empty window. So I loose focus from console, so then must switch windows etc... Can you prevent this? What is the reason to do that?

enebo commented 9 years ago

@PetrKaleta I am not sure but I can see it. Loading AWT might push Java into some idea that it is going to be doing graphics. Off to the internets!

PetrKaleta commented 9 years ago

@enebo did you find something?

enebo commented 9 years ago

@PetrKaleta I might have a solution which will work. Java has a headless mode. In headless mode a series of AWT toolkit classes native impls are basically stubbed out. One side-effect is when headless it does not create the external window like you are seeing. So for batch processing:

jruby -J-Djava.awt.headless=true

or before image_voodoo is required:

java.lang.System.setProperty('java.awt.headless', true)

I am thinking about making a flag in bin/image_voodoo that detects whether preview is used and if so uses non-headless mode but without specifying this flag image_voodoo will by default be in headless mode. This has two points worth mentioning:

  1. If you already have loaded any AWT classes in question then it won't change anything as those classes usage will start Java in non-Java headless mode but I think that is ok.
  2. it means people who use image_voodoo and do want AWT classes used will need to explicitly take an action or image_voodoo will end up starting headless mode. I think this is a vast minority of use cases, but it is still a change in behavior.
enebo commented 9 years ago

This is a big improvement. Thinking about this change a bit more I don't expect my fears of interactive users breaking will be real. For 1) are there any? :) and 2) If they are using AWT then it is extremely likely the first thing they load will be something else before image_voodoo in which case the headless property will have shown up too late to affect behavior.