Compiled jruby application_controller.class can not be found

I am using jruby-1.4.0 to test Ahead-of-Time jruby compiler(jrubyc) to
compile one of my application_controller.rb (in my case the application
is
widgets) into java bytecodes(widgets_controller.class) at the current
controller directory. Once the widgets_controller.class is generated, I
remove the original widgets_controller.rb file from the current
controller
dir, hopefully the widgets_application java class byecode can be called
instead of the ruby source codes. Then I fire the mongrel server from my
jruby application root directory as showing below:

jruby script/server

Mongrel server is running well, I can access my jruby application web
page
like these: http://10.0.0.4:3000/
and the Welcome Message can be shown on the Web Browser:


Welcome aboard
You’re riding Ruby on Rails!
About your application’s environment

Ruby version 1.8.7 (java)
RubyGems version 1.3.5
Rack version 1.0
Rails version 2.3.5
Active Record version 2.3.5
Active Resource version 2.3.5
Action Mailer version 2.3.5
Active Support version 2.3.5
Application root /home/liao1k/testapp
Environment development
Database adapter jdbcmysql
Database schema version 20100105052106

However when try to access my widgets application controller by using
the
url (http://10.0.0.4:3000/widgets), I got following Error Message:

NameError in WidgetsController#index

uninitialized constant WidgetsController
RAILS_ROOT: /home/liao1k/testapp

Application Trace | Framework Trace | Full Trace
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:443:in
load_missing_constant' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in const_missing_with_dependencies’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:92:in
const_missing' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/inflector.rb:361:in constantize’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/inflector.rb:360:in
each' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/inflector.rb:360:in constantize’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/string/inflections.rb:162:in
constantize' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:443:in recognize’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:436:in
call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:87:in dispatch’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:121:in
_call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:130:in build_middleware_stack’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in
call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in call’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in
cache' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:9:in cache’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:28:in
call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in call’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/string_coercion.rb:25:in
call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/head.rb:9:in call’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/methodoverride.rb:24:in
call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:15:in call’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/session/cookie_store.rb:93:in
call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:in call’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in
call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:114:in call’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:34:in
run' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:108:in call’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/static.rb:31:in
call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:48:in call’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in
each' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in call’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/log_tailer.rb:17:in
call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/content_length.rb:13:in call’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/chunked.rb:15:in
call' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/mongrel.rb:64:in process’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:159:in
process_client' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:158:in each’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:158:in
process_client' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:285:in run’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:285:in
initialize' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:285:in new’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:285:in
run' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:268:in initialize’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:268:in
new' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:268:in run’
/home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/mongrel.rb:34:in
run' /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111 /home/liao1k/jruby-1.4.0/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:31:in require’
/home/liao1k/jruby-1.4.0/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require’
script/server:3
Request

Parameters:

None
Show session dump


Response

Headers:

{“Cache-Control”=>“no-cache”,
“Content-Type”=>“”}

It seems jruby can not find the widgets_controller.class file from the
controller directory. I use touch Linux command to create an empty
widgets_controller.rb file in the current controller directory. The
Error
Message is gong and the the widgets pages can be accessed from the Web
Browser. I am not sure how to make widgets_controller.class be called
from
jruby.

Thanks for your help.


View this message in context:
http://old.nabble.com/Compiled-jruby-application_controller.class-can-not-be-found-tp27064829p27064829.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Thu, Jan 7, 2010 at 12:37 PM, liao1k [email protected] wrote:

I am using jruby-1.4.0 to test Ahead-of-Time jruby compiler(jrubyc) to
compile one of my application_controller.rb (in my case the application is
widgets) into java bytecodes(widgets_controller.class) at the current
controller directory. Once the widgets_controller.class is generated, I
remove the original widgets_controller.rb file from the current controller
dir, hopefully the widgets_application java class byecode can be called
instead of the ruby source codes. Then I fire the mongrel server from my
jruby application root directory as showing below:

However when try to access my widgets application controller by using the
url (http://10.0.0.4:3000/widgets), I got following Error Message:

NameError in WidgetsController#index

uninitialized constant WidgetsController
RAILS_ROOT: /home/liao1k/testapp

It seems jruby can not find the widgets_controller.class file from the
controller directory. I use touch Linux command to create an empty
widgets_controller.rb file in the current controller directory. The Error
Message is gong and the the widgets pages can be accessed from the Web
Browser. I am not sure how to make widgets_controller.class be called from
jruby.

Thanks for your help.

The problem is actually in Rails here. Rails searches your app subdirs
for *.rb and loads them that way. Because you’ve replaced the *.rb
with a *.class, it doesn’t ever load your file.

A JRuby user tweeted an interesting hack that might work for you:

After compiling, replace the .rb file contents with require
File.dirname(FILE) + ‘/filename.class’ (or something similar).
This will cause Rails to still see the .rb files, and the .rb files
will just load the .class versions.

It’s a bit hacky, admittedly, but we need to work with Rails core on a
way to make it search for .class as well.

  • Charrlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Couldn’t you put some code in a Rails initializer that changes the way
load_missing_constant
works to automatically make requires for files with .rb also try to
require
the corresponding .class file? Then you are just adding some code to
one
place instead of changing the contents of all your rb files you compile.
I
haven’t tried this but I think it would work.

Can I ask why you are doing the AOT compilation? From what I remember
from
other posts on the mailing list this really doesn’t improve performance
in
any way and might just be more pain than it’s worth for you.

Joe

On Thu, Jan 7, 2010 at 2:18 PM, Charles Oliver N.

Hi Joe,

Thanks for your guys’s reply. The purpose for doing AOT compilation is
to
protect the codes on the application server, so nobody can see the
source
codes.

Kevin

Joseph A. wrote:

Can I ask why you are doing the AOT compilation? From what I remember

jruby application root directory as showing below:
It seems jruby can not find the widgets_controller.class file from the
The problem is actually in Rails here. Rails searches your app subdirs
It’s a bit hacky, admittedly, but we need to work with Rails core on a


View this message in context:
http://old.nabble.com/Compiled-jruby-application_controller.class-can-not-be-found-tp27064829p27070610.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Couldn’t you put some code in a Rails initializer that changes the way

load_missing_constant works to automatically make requires for files
with .rb also try to require the corresponding .class file? Then you
are just adding some code to one place instead of changing the
contents
of all your rb files you compile. I haven’t tried this but I think it
would
work.

Following is the code to get this working. This is for rails 2.3.5, but
I also
had it working in 2.2 - don’t remember if I had to change much.

ActiveSupport::Dependencies.module_eval do
def search_for_file(path_suffix)
path_suffix = path_suffix + ‘.rb’ unless path_suffix.ends_with?
‘.rb’
load_paths.each do |root|
path = File.join(root, path_suffix)
return path if File.file? path
path = File.join(root, path_suffix.sub(/.rb$/,’.class’))
return path.sub(/.class$/,’’) if File.file? path
end
nil
end
end

Rails::Initializer.module_eval do
def load_application_classes
return if $rails_rake_task
if configuration.cache_classes
configuration.eager_load_paths.each do |load_path|
matcher = /\A#{Regexp.escape(load_path)}(.)(?:.rb|.class)\Z/
Dir.glob("#{load_path}/**/
").sort.each do |file|
if matcher === file
require_dependency $1
end
end
end
end
end
end

ActionController::Helpers::ClassMethods.module_eval do
def all_application_helpers
extract =
/^#{Regexp.quote(helpers_dir)}/?(.)_helper.(?:rb|class)$/
Dir["#{helpers_dir}/**/
_helper.*"].map { |file|
file.sub extract, ‘\1’ if extract === file
}.compact
end
end


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Daniel,

It works pretty well on jruby1.4. Thank you so much.

Kevin

Daniel S. wrote:

  path = File.join(root, path_suffix)
return if $rails_rake_task

end
end


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


View this message in context:
http://old.nabble.com/Compiled-jruby-application_controller.class-can-not-be-found-tp27064829p27078195.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email