Newb: How do I debug a NameError?

Hello List.

I am trying to port a simple Java demo to a JRuby file.

The Java demo is listed here:

http://gist.github.com/512429

Currently I have written this:

http://gist.github.com/512430

Here is what I see when I run my JRuby file:

$ jruby main.rb
main.rb:11:in `main’: uninitialized constant Main::SampleFrame
(NameError)
from main.rb:23

If this were a plain Ruby script I’d think that I forgot declarations:

require and include

When I look for SampleFrame.class I do see it in the directory right
next to my JRuby script:

$ ls -la SampleFrame*

-rw-r–r-- 1 maco staff 615 Aug 6 19:05 SampleFrame$1.class
-rw-r–r-- 1 maco staff 627 Aug 6 19:05 SampleFrame$10.class
-rw-r–r-- 1 maco staff 623 Aug 6 19:05 SampleFrame$11.class
-rw-r–r-- 1 maco staff 616 Aug 6 19:05 SampleFrame$12.class
-rw-r–r-- 1 maco staff 622 Aug 6 19:05 SampleFrame$13.class
-rw-r–r-- 1 maco staff 635 Aug 6 19:05 SampleFrame$14.class
-rw-r–r-- 1 maco staff 620 Aug 6 19:05 SampleFrame$15.class
-rw-r–r-- 1 maco staff 619 Aug 6 19:05 SampleFrame$16.class
-rw-r–r-- 1 maco staff 620 Aug 6 19:05 SampleFrame$17.class
-rw-r–r-- 1 maco staff 624 Aug 6 19:05 SampleFrame$18.class
-rw-r–r-- 1 maco staff 622 Aug 6 19:05 SampleFrame$19.class
-rw-r–r-- 1 maco staff 618 Aug 6 19:05 SampleFrame$2.class
-rw-r–r-- 1 maco staff 620 Aug 6 19:05 SampleFrame$20.class
-rw-r–r-- 1 maco staff 624 Aug 6 19:05 SampleFrame$21.class
-rw-r–r-- 1 maco staff 622 Aug 6 19:05 SampleFrame$22.class
-rw-r–r-- 1 maco staff 625 Aug 6 19:05 SampleFrame$23.class
-rw-r–r-- 1 maco staff 622 Aug 6 19:05 SampleFrame$24.class
-rw-r–r-- 1 maco staff 625 Aug 6 19:05 SampleFrame$25.class
-rw-r–r-- 1 maco staff 626 Aug 6 19:05 SampleFrame$26.class
-rw-r–r-- 1 maco staff 624 Aug 6 19:05 SampleFrame$27.class
-rw-r–r-- 1 maco staff 625 Aug 6 19:05 SampleFrame$28.class
-rw-r–r-- 1 maco staff 614 Aug 6 19:05 SampleFrame$29.class
-rw-r–r-- 1 maco staff 618 Aug 6 19:05 SampleFrame$3.class
-rw-r–r-- 1 maco staff 614 Aug 6 19:05 SampleFrame$30.class
-rw-r–r-- 1 maco staff 621 Aug 6 19:05 SampleFrame$4.class
-rw-r–r-- 1 maco staff 619 Aug 6 19:05 SampleFrame$5.class
-rw-r–r-- 1 maco staff 622 Aug 6 19:05 SampleFrame$6.class
-rw-r–r-- 1 maco staff 622 Aug 6 19:05 SampleFrame$7.class
-rw-r–r-- 1 maco staff 628 Aug 6 19:05 SampleFrame$8.class
-rw-r–r-- 1 maco staff 623 Aug 6 19:05 SampleFrame$9.class
-rw-r–r-- 1 maco staff 21698 Aug 6 19:05 SampleFrame.class
-rw-r–[email protected] 1 maco staff 33436 Apr 30 05:03 SampleFrame.java

One obvious thing I tried was adding this declaration:

include_class ‘SampleFrame’

So, now I have this:

http://gist.github.com/512443

When I do that I see this:

$ jruby main.rb
/pt/jruby/jruby-1.5.1/lib/ruby/site_ruby/shared/builtin/javasupport/core_ext/object.rb:26:in
`include_class’: cannot link Java class SampleFrame, probable missing
dependency: SampleFrame (wrong name: TestJavaClient/SampleFrame)
(NameError)
from main.rb:8

I’m new to JRuby so I’d welcome any comments on how to understand this
error message.

Thanks!


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Fri, Aug 6, 2010 at 11:47 PM, Audrey L. [email protected]
wrote:

$ jruby main.rb
/pt/jruby/jruby-1.5.1/lib/ruby/site_ruby/shared/builtin/javasupport/core_ext/object.rb:26:in
`include_class’: cannot link Java class SampleFrame, probable missing
dependency: SampleFrame (wrong name: TestJavaClient/SampleFrame)
(NameError)
    from main.rb:8

I’m new to JRuby so I’d welcome any comments on how to understand this
error message.

It looks like the SampleFrame class was compiled with a package of
TestJavaClient, and so when you try to load it as “SampleFrame”, it
blows up. Java is very picky about packages. If you would like
SampleFrame to live under the TestJavaClient package, then the class
files must eventually live under a TestJavaClient/ directory. If you
move these classes into a TestJavaClient/ subdirectory and load it
with include_class “TestJavaClient.SampleFrame” it should work like
you want.

FYI, “java_import” is the standard way we recommend importing classes.
“include_class” works identically, but it’s longer and mildly
deprecated. “import” also works, but it’s a common-enough method in
Ruby-land that it often conflicts with third-party code.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hello List,

Here is a copy of a java file which has captured my interest:

http://gist.github.com/512429

I want to use it and the JRuby community to learn more about Java (and
JRuby too!)

Here is my current effort to port the above Java file to a JRuby file:

http://gist.github.com/513628
(Thanks Charlie!)

In addition to writing the JRuby file, I placed it in the directory
above the Java files it depends upon.

The Java files live in a directory named: “TestJavaClient”

When I use JRuby to run my main.rb file I see the following:

Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$ cat main.rb
#!/usr/bin/env jruby

main.rb

require ‘java’

java_import ‘java.awt.Component’
java_import ‘javax.swing.JOptionPane’
java_import ‘javax.swing.SwingUtilities’
java_import ‘TestJavaClient’

class Main
java_signature ‘void main(String[])’
def self.main(args)
sample_frame = SampleFrame.new
sample_frame.set_visible(true)
end

java_signature ‘void inform( final Component parent, final String
str)’
def self.inform(parent, str)
if SwingUtilities.event_dispatch_thread?
show_msg parent, str, JOptionPane::INFORMATION_MESSAGE
else
SwingUtilities.invoke_later {
show_msg parent, str, JOptionPane.INFORMATION_MESSAGE
}
end
end

end

Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$
Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$
Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$ ll
drwxr-xr-x 122 maco staff 4148 Aug 7 22:25 TestJavaClient/
-rwxr-xr-x 1 maco staff 684 Aug 7 22:34 main.rb*
Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$
Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$

Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$
Sat Aug 07 22:40 /pt/trading/api/IBJts/java maco$
Sat Aug 07 22:40 /pt/trading/api/IBJts/java maco$ ll
TestJavaClient/SampleFrame*
-rw-r–r-- 1 maco staff 615 Aug 6 19:05
TestJavaClient/SampleFrame$1.class
-rw-r–r-- 1 maco staff 627 Aug 6 19:05
TestJavaClient/SampleFrame$10.class
-rw-r–r-- 1 maco staff 623 Aug 6 19:05
TestJavaClient/SampleFrame$11.class
-rw-r–r-- 1 maco staff 622 Aug 6 19:05
TestJavaClient/SampleFrame$6.class
-rw-r–r-- 1 maco staff 622 Aug 6 19:05
TestJavaClient/SampleFrame$7.class
-rw-r–r-- 1 maco staff 628 Aug 6 19:05
TestJavaClient/SampleFrame$8.class
-rw-r–r-- 1 maco staff 623 Aug 6 19:05
TestJavaClient/SampleFrame$9.class
-rw-r–r-- 1 maco staff 21698 Aug 6 19:05
TestJavaClient/SampleFrame.class
-rw-r–[email protected] 1 maco staff 33436 Apr 30 05:03
TestJavaClient/SampleFrame.java
Sat Aug 07 22:41 /pt/trading/api/IBJts/java maco$
Sat Aug 07 22:41 /pt/trading/api/IBJts/java maco$

Sat Aug 07 22:41 /pt/trading/api/IBJts/java maco$
Sat Aug 07 22:41 /pt/trading/api/IBJts/java maco$ jruby main.rb
main.rb:8: cannot load Java class TestJavaClient (NameError)
Sat Aug 07 22:41 /pt/trading/api/IBJts/java maco$
Sat Aug 07 22:42 /pt/trading/api/IBJts/java maco$

I’m not sure how to debug this.

I assume that this declaration:

java_import ‘TestJavaClient’

will import all the classes in the ‘TestJavaClient’ package.

Also I assume that the files which hold the java classes need to live
in a directory which has the name ‘TestJavaClient’

And I assume that main.rb, my JRuby file, needs to see
‘TestJavaClient’ as a subdirectory.

Are these a correct assumptions?

On 8/7/10, Charles Oliver N. [email protected] wrote:

When I do that I see this:
error message.
FYI, “java_import” is the standard way we recommend importing classes.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Tommy,

Thanks for helping me with this!

You are a gentleman who deserves free beer at all sporting events.

I inspected the package declaraion of SampleFrame.java.

I see this:

package TestJavaClient;

Based on my very limited knowledge about Java, this means that
SampleFrame.java MUST live in a directory named ‘TestJavaClient’.

I do not have the freedom to place this Java software in a
lowercase-named-directory as you suggest.

Let me know your thoughts on this; perhaps I have an incorrect
assumption about this.

Next, I tried your 1st idea.

I changed
java_import ‘SampleFrame’
to
java_import ‘TestJavaClient.SampleFrame’

I now have this:

http://gist.github.com/514557

And I see this:

Sun Aug 08 14:26 /pt/trading/api/IBJts/java maco$
Sun Aug 08 14:26 /pt/trading/api/IBJts/java maco$ grep import main.rb
java_import ‘java.awt.Component’
java_import ‘javax.swing.JOptionPane’
java_import ‘javax.swing.SwingUtilities’
java_import ‘TestJavaClient.SampleFrame’
Sun Aug 08 14:26 /pt/trading/api/IBJts/java maco$
Sun Aug 08 14:26 /pt/trading/api/IBJts/java maco$ jruby main.rb
Sun Aug 08 14:27 /pt/trading/api/IBJts/java maco$
Sun Aug 08 14:27 /pt/trading/api/IBJts/java maco$

As you can see, the file briefly ran for a second and then exited.

Also I saw a white-coffe-cup bounce in my dock during that 1 second.

I think this is a good thing because the error is now gone.

So, it looks like the issue I raised with this post (Newb: How do I
debug a NameError ?), is resolved. Yay!

The behavior of the script, however, is not what I want.

I want the script to behave as this Java file behaves:

http://gist.github.com/514573

If anyone is bored today and in need of both kind words and complements,

Please offer any opinions on how to make this file:

http://gist.github.com/514557
behave like this file:
http://gist.github.com/514573

I have some brain-power. If I can figure this out on my own I will
share.

Thanks!

On 8/8/10, Tommy C. [email protected] wrote:

are lower case.

Here is a copy of a java file which has captured my interest:

require ‘java’
sample_frame.set_visible(true)
end
Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$
TestJavaClient/SampleFrame$10.class
-rw-r–r-- 1 maco staff 21698 Aug 6 19:05
Sat Aug 07 22:41 /pt/trading/api/IBJts/java maco$
Also I assume that the files which hold the java classes need to live

wrote:

$ jruby main.rb
TestJavaClient, and so when you try to load it as “SampleFrame”, it
Ruby-land that it often conflicts with third-party code.



To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

I think java_import ‘TestJavaClient’ will import the class. The last
term is the class name. Notice in

java_import ‘javax.swing.JOptionPane’
JOptionPane is a class, not a directory

It looks like you want to import SampleFrame so you want to try
java_import ‘TestJavaClient.SampleFrame’

rename TestJavaClient to lower case to reflect that package directories
are lower case.

@tommychheng
Programmer and UC Irvine Graduate Student
Find a great grad school based on research interests:
http://gradschoolnow.com

On 8/7/10 11:09 PM, Audrey L. wrote:

Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$ cat main.rb
java_signature ‘void main(String[])’
SwingUtilities.invoke_later {
Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$ ll
-rw-r–r-- 1 maco staff 627 Aug 6 19:05
Sat Aug 07 22:41 /pt/trading/api/IBJts/java maco$
I assume that this declaration:

So, now I have this:
from main.rb:8
with include_class “TestJavaClient.SampleFrame” it should work like
To unsubscribe from this list, please visit:


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

BTW,

if anyone wants to download the actual software, be my guest:

http://github.com/AudreyLeeMe/ib_java_files/archives/master

This software allows you to lose money at internet speed.

It is designed to demo a Java API to interact with servers run by
Interactive Brokers (IB).

So, if you have some kind of trading idea which can be implemented as
a piece of software, this API helps follow that bunny trail.

IB does offer ‘paper-trading’ to IB customers.

So my plan is to write some software (using JRuby) and then run it
against their servers using a paper-trading account.

More info:

http://www.google.com/search?q=api+site:interactivebrokers.com

On 8/8/10, Audrey L. [email protected] wrote:

package TestJavaClient;
Next, I tried your 1st idea.
And I see this:
Sun Aug 08 14:27 /pt/trading/api/IBJts/java maco$
debug a NameError ?), is resolved. Yay!

I think java_import ‘TestJavaClient’ will import the class. The last

In addition to writing the JRuby file, I placed it in the directory

end
end
Sat Aug 07 22:39 /pt/trading/api/IBJts/java maco$
TestJavaClient/SampleFrame$10.class
-rw-r–r-- 1 maco staff 21698 Aug 6 19:05
Sat Aug 07 22:41 /pt/trading/api/IBJts/java maco$
Also I assume that the files which hold the java classes need to live

wrote:

$ jruby main.rb
TestJavaClient, and so when you try to load it as “SampleFrame”, it
Ruby-land that it often conflicts with third-party code.



To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Sun, Aug 8, 2010 at 4:38 PM, Audrey L. [email protected]
wrote:

If anyone is bored today and in need of both kind words and complements,

Please offer any opinions on how to make this file:

http://gist.github.com/514557
behave like this file:
http://gist.github.com/514573

Ok, I think there’s some confusion here.

Normally when running a Ruby script, the body of the script is
equivalent to “main” in Java. So if you want to run with the normal
“jruby” command, you would need to do one of two things:

  • Put the code in your Main.main method in the body of the script
  • Call Main.main([]) at the bottom of the script

This is why you’re only seeing the coffee cup pop up briefly…the
script itself is not actually running Main.main, so the frame is never
created and no window pops up.

The java_signature stuff you’ve added is normally only needed if you
want to generate a Java class from Ruby code. This is not usually
necessary if you’re just running scripts, since JRuby doesn’t require
that you compile your Ruby code or generate Java classes.

If you want to generate a Java class, you would use jrubyc --java or
jruby --javac to generate it. I modified your code to just use
javax.swing.JFrame instead of SimpleFrame, and was then able to
compile it like this:

~/projects/jruby âž” jrubyc --javac simple_swing.rb
Generating Java class Main to /Users/headius/projects/jruby/Main.java
javac -d /Users/headius/projects/jruby -cp
/Users/headius/projects/jruby/lib/jruby.jar:.
/Users/headius/projects/jruby/Main.java

And I could run it like this:

~/projects/jruby âž” java -cp lib/jruby.jar:. Main

This worked like you want…a small frame popped up and the
application continued running.

Note that the jrubyc --java© stuff (and the java_signature stuff) is
only if you really need to generate a Java class from a Ruby class.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs