Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


JRuby compatibility #14

merged 3 commits into from

10 participants


Hey Sam, I would really enjoy a JRuby compatible version of the yui-compressor gem ...

Now I do not think invoking the compiler from the same process on JRuby is the way since compilers such as YUI and Closure tend to do a System.exit after their done ... but still there's a simple way of doing it "your way" - creating a separate java -jar process - without using POpen4 which does a fork and thus it's having a hard-time running on JRuby ...

Tests pass on latest JRuby 1.6 as well as MRI 1.8.7 and 1.9.2 (and should work on Windows as well).


@kares In case you're interested, I wrote a gem that uses native Java I/O writers on JRuby. You can find it here:


@mjijackson excellent but this one still makes sense for people who do not want to "bloat-in" the YUI classes but rather do their compilation as a one time thing ... btw. You're lucky to do that with YUI, as I tried smt similar with Closure and ended up being frustrated from System.exit calls baked into a few crucial places ...


+1! Yes, please!



I'm using JRuby on my production machine and using uglifier because yui-compressor do not work on JRuby :(






I like the idea, but I think we really do want that error stream. In fact there's another pull request open to include the STDERR output in the exception when it fails.

I added a branch 'childprocess' where I'm experimenting with as a way to support jruby. Please give it a try if you're interested. I don't have any serious jruby environments to try out these changes, although I can say the unit tests are passing under jruby 1.7.3.


Seems to just hang once it reaches application.css on my jruby 1.7.4 app


This is the process that is being run:

/usr/bin/java -jar /Users/hansolo/.rvm/gems/jruby-1.7.4/bundler/gems/ruby-yui-compressor-112a0fb4bcab/lib/yui/../yuicompressor-2.4.7.jar --type css --charset utf-8

It also hangs if I run it manually. Should it be receiving another argument with the css that it needs to compress?


The more I look into it, the more I like this approach. The only problem is it doesn't give you the error stream in any version of jruby I've tried, but it does work and you do get the error stream in all the MRI versions I've tried. It also gets rid of a gem dependency which is a good thing.

I've only got a Linux box on hand so I'll find a way to test this on OSX and Windows. If it goes well, then this can go in 0.11.0.

@stevecrozz stevecrozz merged commit 9e0a558 into sstephenson:master

Testing went pretty well, so I went ahead and merged this in. The error stream is somehow lost in JRuby on Linux. In every other environment I tested, it gets written to the parent process' STDERR, including JRuby on Windows. But I think that's ok. It's certainly better than full brokenness.

This was referenced
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 19, 2011
  1. @kares

    replace POpen4 with a Tempfile - thus making it compatible with JRuby…

    kares authored
    … (tested on Linux with JRuby 1.6.0)
  2. @kares
Commits on Jun 17, 2011
  1. @kares
This page is out of date. Refresh to see the latest.
Showing with 13 additions and 27 deletions.
  1. +1 −1  README.rdoc
  2. +12 −25 lib/yui/compressor.rb
  3. +0 −1  yui-compressor.gemspec
2  README.rdoc
@@ -2,7 +2,7 @@
Ruby-YUI Compressor provides a Ruby interface to the {YUI Compressor Java library}[] for minifying JavaScript and CSS assets.
-<b>Latest version:</b> 0.9.4 (includes YUI Compressor 2.4.4)
+<b>Latest version:</b> 0.9.6 (includes YUI Compressor 2.4.4)
* {API documentation}[]
* {Source code}[]
37 lib/yui/compressor.rb
@@ -1,6 +1,6 @@
-require "popen4"
require "shellwords"
require "stringio"
+require "tempfile"
module YUI #:nodoc:
class Compressor
@@ -64,24 +64,19 @@ def command #:nodoc:
def compress(stream_or_string)
streamify(stream_or_string) do |stream|
- output = true
- status = POpen4.popen4(command, "b") do |stdout, stderr, stdin, pid|
- begin
- stdin.binmode
- transfer(stream, stdin)
- if block_given?
- yield stdout
- else
- output =
- end
- rescue Exception => e
- raise RuntimeError, "compression failed"
- end
+ tempfile ='yui_compress')
+ tempfile.write
+ tempfile.flush
+ begin
+ output = `#{command} #{tempfile.path}`
+ rescue Exception
+ raise RuntimeError, "compression failed"
+ ensure
+ tempfile.close!
- if
+ if $?
raise RuntimeError, "compression failed"
@@ -118,14 +113,6 @@ def streamify(stream_or_string)
- def transfer(from_stream, to_stream)
- while buffer =
- to_stream.write(buffer)
- end
- from_stream.close
- to_stream.close
- end
def command_option_for_type
["--type", self.class.compressor_type.to_s]
1  yui-compressor.gemspec
@@ -11,5 +11,4 @@ do |s|
s.authors = ["Sam Stephenson"]
s.files = Dir["Rakefile", "lib/**/*", "test/**/*"]
s.test_files = Dir["test/*_test.rb"] unless $SAFE > 0
- s.add_dependency "POpen4", ">= 0.1.4"
Something went wrong with that request. Please try again.