Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoMethodError: undefined method `current' for Java::JavaLang::Thread:Class #4897

Closed
javalovercn opened this issue Dec 16, 2017 · 1 comment
Closed

Comments

@javalovercn
Copy link

ScriptingContainer c = new ScriptingContainer();

c.runScriptlet("#this is script A\n" +
		"puts Thread.current.to_s");

System.out.println("============================");

c.runScriptlet("#this is script B\n" +
		"import java.lang.Thread\n" +
		"puts 'hello'");

System.out.println("============================");

c.runScriptlet("#this is script C\n" +
		"puts Thread.current.to_s");

Console output

#<Thread:0x5782ce4d>
============================
hello
============================
uri:classloader:/jruby/java/core_ext/object.rb:95: warning: already initialized constant Thread
NoMethodError: undefined method `current' for Java::JavaLang::Thread:Class
Did you mean?  currentThread
  method_missing at org/jruby/RubyBasicObject.java:1657
          <main> at <script>:2
Exception in thread "main" org.jruby.embed.EvalFailedException: (NoMethodError) undefined method `current' for Java::JavaLang::Thread:Class
Did you mean?  currentThread
	at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:131)
	at org.jruby.embed.ScriptingContainer.runUnit(ScriptingContainer.java:1307)
	at org.jruby.embed.ScriptingContainer.runScriptlet(ScriptingContainer.java:1300)
	at com.TestThread.main(TestThread.java:22)
Caused by: org.jruby.exceptions.RaiseException: (NoMethodError) undefined method `current' for Java::JavaLang::Thread:Class
Did you mean?  currentThread
	at org.jruby.RubyBasicObject.method_missing(org/jruby/RubyBasicObject.java:1657)
	at RUBY.<main>(<script>:2)

Expected output

#<Thread:0x5782ce4d>
============================
hello
============================
#<Thread:0x5782ce4d>

Environment

jruby-complete-9.1.15.0.jar

@kares
Copy link
Member

kares commented Dec 16, 2017

you import import java.lang.Thread Java's thread into default Object name-space
... so you end up aliasing Ruby's Thread to java.lang.Thread than there's no current in Java's world
the warnings/messages are pretty clear about it :

warning: already initialized constant Thread
undefined method `current' for Java::JavaLang::Thread

remove the import line and Thread.current will work as (Ruby) expected

@kares kares added this to the Invalid or Duplicate milestone Dec 16, 2017
@kares kares closed this as completed Dec 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants