|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
JRuby Cookbook example 1.10I can not seem to get Example 1.10 to work using Netbeans 6.51 or 6.7. I create a Java Application project in Netbeans and pasted the code from the book in verbatim.
I was finally able to resolve org.jruby.Ruby by specifically adding C:\Program Files\NetBeans 6.7\ruby2\jruby-1.2.0\lib\jruby.jar to the library options for the project - not ideal I think but it works But that still left me with these errors: package org.jrubycookbook.ch01; // incorrect package name - also, is this perhaps where PrintJavaClass should reside? public class PrintJavaClass // PrintJavaClass is public, it should be declared in a file named PrintJavaClass.java List<Class> interfaces = Arrays.asList(o.getClass().getInterfaces()); // Incompatible types required java.util.List<java.lang.Class> found java.util.List<java.lang.Class<?>> What environment where you using where you didn't get these errors? Carl Graff wrote: > > Actually I did find instances of the jruby.jar but I think for some reason these are not considered path of the default classpath > > C:\Program Files\NetBeans 6.7\ruby2\jruby-1.2.0\lib\jruby.jar\org\jruby\Ruby.class > C:\Program Files\NetBeans 6.5\ruby2\jruby-1.1.6\lib\jruby.jar\org\jruby\ Ruby.class > > Carl Graff wrote: >> Hi, >> >> Unfortunately I only got to example 1.10 before getting stuck. I have used both Ruby and Java for several years but it is a little disheartening to get bogged down this early in the book. >> >> The sample code is below and here are some of my questions about it: >> 1. Most importantly is is choking on >> import org.jruby.Ruby; >> I found a jruby.Jar on the JRuby site but I am using a complete Netbeans 6.7 download (Java and Ruby) and also tried adding Ruby and JRuby plugins to a 6.5 build. Nether of these builds know what to do with >> import org.jruby.Ruby; >> Do I need to download JRuby code - in addition - to the JRuby code and libraries provided by NetBeans? If so do I need to put this code in a special directory or add an environment setting or a netbeans option so it get recognized? >> >> >> 2. The statement >> package org.jrubycookbook.ch01; Leads me to believe that either I need to download some associated sample programs or libraries or the I need to put this code in a specific directory relative to netbeans or perhaps relative to a separate installation of JRuby if that is needed - is either of these statements correct? If I need to download sample code or libraries the book does not seem to have a link to a download area. >> >> 3. The text preceding the sample indicates >> Ruby Array objects can also be coerced into Java Array objects by calling the to_java method. Example 1-10 <javascript:moveTo('ruby_to_java_type_conversion');> includes a combination of Java and Ruby code, which demonstrates this functionality. >> But I see no call to "to_java" in the sample code. >> >> package org.jrubycookbook.ch01; >> >> import java.io.PrintWriter; >> import java.io.StringWriter; >> import java.util.Arrays; >> import java.util.Collections; >> import java.util.List; >> >> import org.jruby.Ruby; >> import org.jruby.javasupport.JavaEmbedUtils; >> >> public class PrintJavaClass { >> >> // Output the class and interface list for a single object >> public String output(Object o) { >> String className = o.getClass().getName(); >> List<Class> interfaces = Arrays.asList(o.getClass().getInterfaces()); >> >> return String.format("%s, implements %s\n", className, interfaces); >> } >> >> // Output the class and interface list for each object in an array >> public String output(Object[] objects) { >> PrintWriter writer = new PrintWriter(new StringWriter()); >> for (Object o : objects) { >> String className = o.getClass().getName(); >> List<Class> interfaces = Arrays >> .asList(o.getClass().getInterfaces()); >> >> writer.printf("%s (inside array), implements %s\n", className, >> interfaces); >> } >> return writer.toString(); >> } >> >> public static void main(String[] args) { >> Ruby runtime = JavaEmbedUtils.initialize(Collections.EMPTY_LIST); >> String script = "@printer = org.jrubycookbook.ch01.PrintJavaClass.new\n" >> + "def output(o)\n" >> + "puts \"#{o.to_s} - #{@printer.output(o)}\"\n" >> + "end\n" >> + "output(1)\n" >> + "output(0.5)\n" >> + "output('string')\n" >> + "output(true)\n" >> + "output([4, 8, 15, 16, 23, 42])\n" >> + "output([4, 8, 15, 16, 23, 42].to_java)\n" >> + "output({ 'NY' => 'New York', 'MA' => 'Massachusetts'})\n"; >> >> runtime.evalScriptlet(script); >> JavaEmbedUtils.terminate(runtime); >> } >> } |
|
|
Re: JRuby Cookbook example 1.10Hi,
On Mon, Jun 29, 2009 at 10:41 PM, cag<cagraff@...> wrote: > > I can not seem to get Example 1.10 to work using Netbeans 6.51 or 6.7. I > create a Java Application project in Netbeans and pasted the code from the > book in verbatim. This sample worked as far as I tried although the output was bit different shown in the book. > > I was finally able to resolve org.jruby.Ruby by specifically adding > C:\Program Files\NetBeans 6.7\ruby2\jruby-1.2.0\lib\jruby.jar > to the library options for the project - not ideal I think but it works You did in a very common way as described at http://www.netbeans.org/kb/docs/java/project-setup.html#projects-classpath. > > But that still left me with these errors: > > package org.jrubycookbook.ch01; // incorrect package name - also, is this > perhaps where PrintJavaClass should reside? Package hierarchies are mapped to directory(folder) hierarchies. The example has org.jrubyvookbook.ch01 for its package name, so PrintJavaClass.java file must reside under the org\jrubycookbook\ch01 directory. Probably, http://java.sun.com/docs/books/tutorial/java/package/managingfiles.html would help you to figure out what the package is. Since you are using NetBeans, you don't need to create folders by hand. http://www.netbeans.org/kb/docs/java/javase-intro.html might be good for you. > > public class PrintJavaClass // PrintJavaClass is public, it should be > declared in a file named PrintJavaClass.java Yes. Public classes should be in the same filenames, so your program should be written in org\jrubycookbook\ch01\PrintJavaClass.java. You can found a lot of information by googling since this is a basic part of Java. > > List<Class> interfaces = Arrays.asList(o.getClass().getInterfaces()); // > Incompatible types required java.util.List<java.lang.Class> found > java.util.List<java.lang.Class<?>> I'm not sure why you got this error. The error might be resolved if you could set up your Java project correctly on NetBeans. -Yoko > > > What environment where you using where you didn't get these errors? > > > Carl Graff wrote: >> >> Actually I did find instances of the jruby.jar but I think for some reason >> these are not considered path of the default classpath >> >> C:\Program Files\NetBeans >> 6.7\ruby2\jruby-1.2.0\lib\jruby.jar\org\jruby\Ruby.class >> C:\Program Files\NetBeans 6.5\ruby2\jruby-1.1.6\lib\jruby.jar\org\jruby\ >> Ruby.class >> >> Carl Graff wrote: > >>> Hi, >>> >>> Unfortunately I only got to example 1.10 before getting stuck. I have >>> used both Ruby and Java for several years but it is a little >>> disheartening to get bogged down this early in the book. >>> >>> The sample code is below and here are some of my questions about it: >>> 1. Most importantly is is choking on >>> import org.jruby.Ruby; >>> I found a jruby.Jar on the JRuby site but I am using a complete Netbeans >>> 6.7 download (Java and Ruby) and also tried adding Ruby and JRuby plugins >>> to a 6.5 build. Nether of these builds know what to do with >>> import org.jruby.Ruby; >>> Do I need to download JRuby code - in addition - to the JRuby code and >>> libraries provided by NetBeans? If so do I need to put this code in a >>> special directory or add an environment setting or a netbeans option so >>> it get recognized? >>> >>> >>> 2. The statement >>> package org.jrubycookbook.ch01; Leads me to believe that either I need >>> to download some associated sample programs or libraries or the I need to >>> put this code in a specific directory relative to netbeans or perhaps >>> relative to a separate installation of JRuby if that is needed - is >>> either of these statements correct? If I need to download sample code or >>> libraries the book does not seem to have a link to a download area. >>> >>> 3. The text preceding the sample indicates >>> Ruby Array objects can also be coerced into Java Array objects by calling >>> the to_java method. Example 1-10 >>> <javascript:moveTo('ruby_to_java_type_conversion');> includes a >>> combination of Java and Ruby code, which demonstrates this functionality. >>> But I see no call to "to_java" in the sample code. >>> >>> package org.jrubycookbook.ch01; >>> >>> import java.io.PrintWriter; >>> import java.io.StringWriter; >>> import java.util.Arrays; >>> import java.util.Collections; >>> import java.util.List; >>> >>> import org.jruby.Ruby; >>> import org.jruby.javasupport.JavaEmbedUtils; >>> >>> public class PrintJavaClass { >>> >>> // Output the class and interface list for a single object >>> public String output(Object o) { >>> String className = o.getClass().getName(); >>> List<Class> interfaces = >>> Arrays.asList(o.getClass().getInterfaces()); >>> >>> return String.format("%s, implements %s\n", className, >>> interfaces); >>> } >>> >>> // Output the class and interface list for each object in an array >>> public String output(Object[] objects) { >>> PrintWriter writer = new PrintWriter(new StringWriter()); >>> for (Object o : objects) { >>> String className = o.getClass().getName(); >>> List<Class> interfaces = Arrays >>> .asList(o.getClass().getInterfaces()); >>> >>> writer.printf("%s (inside array), implements %s\n", className, >>> interfaces); >>> } >>> return writer.toString(); >>> } >>> >>> public static void main(String[] args) { >>> Ruby runtime = JavaEmbedUtils.initialize(Collections.EMPTY_LIST); >>> String script = "@printer = >>> org.jrubycookbook.ch01.PrintJavaClass.new\n" >>> + "def output(o)\n" >>> + "puts \"#{o.to_s} - #{@printer.output(o)}\"\n" >>> + "end\n" >>> + "output(1)\n" >>> + "output(0.5)\n" >>> + "output('string')\n" >>> + "output(true)\n" >>> + "output([4, 8, 15, 16, 23, 42])\n" >>> + "output([4, 8, 15, 16, 23, 42].to_java)\n" >>> + "output({ 'NY' => 'New York', 'MA' => >>> 'Massachusetts'})\n"; >>> >>> runtime.evalScriptlet(script); >>> JavaEmbedUtils.terminate(runtime); >>> } >>> } > > > > -- > View this message in context: http://www.nabble.com/JRuby-Cookbook-example-1.10-tp24263046p24263046.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 > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: JRuby Cookbook example 1.10Thank you Yoko for the careful evaluation and reply to my questions.
I think the one thing that I might be missing in all this is where you got the JARs or sample code for the solution. I am reading this book though my Safari books subscription and can not find the chapter based sample code nor the code implementing "PrintJavaClass". Did you have sample code or create your own "PrintJavaClass"? Did you obtain any sample code for each chapter? If it is case that I simple need to create a package called org\jrubycookbook\ch01\PrintJavaClass.java I guess I have a lot to learn about creating "empty/new" package paths. Perhaps you would be kind enough to show me the correct way to create the package org\jrubycookbook\ch01.
|
|
|
Re: JRuby Cookbook example 1.10One last thing I should mention is that I created this as a netbeans Java Application. I assume that this is the correct project type. I actually created two of these Java Application projects - one simply defaulted to JavaApplication1 and one named PrintJavaClass.
There are other project types including Java Desktop Application and Java Class Library and Java Free-Forn Project. Perhaps I should have named the project org\jrubycookbook\ch01\PrintJavaClass
|
|
|
Re: JRuby Cookbook example 1.10On Wed, Jul 1, 2009 at 2:35 PM, cag<cagraff@...> wrote:
> > One last thing I should mention is that I created this as a netbeans Java > Application. I assume that this is the correct project type. I actually > created two of these Java Application projects - one simply defaulted to > JavaApplication1 and one named PrintJavaClass. > > There are other project types including Java Desktop Application and Java > Class Library and Java Free-Forn Project. > > Perhaps I should have named the project > org\jrubycookbook\ch01\PrintJavaClass Example 1-10 in JRuby Cookbook is an oridnary Java program, so you need a project whose type is Java Application on NetBeans. http://www.netbeans.org/kb/docs/java/quickstart.html would be a good tutorial to start Java programming on NetBeans. In general, a project name doesn't matter, so as this case. Thus, you can name the project whatever you like. After, you create a Java Application project, you need to create the package whose name is org.jrubycookbook.ch01 on a project window on NetBeans. NetBeans' quick start tutorial explains how you can do this. Then, you need to create a Java class, PrintJavaClass, under the package org.jrubycookbook.ch01. Also, NetBeans' quick start tutorial shows how you can do this. Example 1-10 itself is PrintJavaClass, so you don't need any other classes. You just need jruby.jar in the libraries section of your project on NetBeans. -Yoko > > > cag wrote: >> >> Thank you Yoko for the careful evaluation and reply to my questions. >> I think the one thing that I might be missing in all this is where you got >> the JARs or sample code for the solution. I am reading this book though my >> Safari books subscription and can not find the chapter based sample code >> nor the code implementing "PrintJavaClass". >> >> Did you have sample code or create your own "PrintJavaClass"? Did you >> obtain any sample code for each chapter? >> If it is case that I simple need to create a package called >> org\jrubycookbook\ch01\PrintJavaClass.java I guess I have a lot to learn >> about creating "empty/new" package paths. Perhaps you would be kind enough >> to show me the correct way to create the package org\jrubycookbook\ch01. >> >> >> >> >> >> Yoko Harada wrote: >>> >>> Hi, >>> >>> On Mon, Jun 29, 2009 at 10:41 PM, cag<cagraff@...> wrote: >>>> >>>> I can not seem to get Example 1.10 to work using Netbeans 6.51 or 6.7. I >>>> create a Java Application project in Netbeans and pasted the code from >>>> the >>>> book in verbatim. >>> >>> This sample worked as far as I tried although the output was bit >>> different shown in the book. >>> >>>> >>>> I was finally able to resolve org.jruby.Ruby by specifically adding >>>> C:\Program Files\NetBeans 6.7\ruby2\jruby-1.2.0\lib\jruby.jar >>>> to the library options for the project - not ideal I think but it works >>> >>> You did in a very common way as described at >>> http://www.netbeans.org/kb/docs/java/project-setup.html#projects-classpath. >>> >>>> >>>> But that still left me with these errors: >>>> >>>> package org.jrubycookbook.ch01; // incorrect package name - also, is >>>> this >>>> perhaps where PrintJavaClass should reside? >>> >>> Package hierarchies are mapped to directory(folder) hierarchies. The >>> example has org.jrubyvookbook.ch01 for its package name, so >>> PrintJavaClass.java file must reside under the org\jrubycookbook\ch01 >>> directory. Probably, >>> http://java.sun.com/docs/books/tutorial/java/package/managingfiles.html >>> would help you to figure out what the package is. >>> Since you are using NetBeans, you don't need to create folders by >>> hand. http://www.netbeans.org/kb/docs/java/javase-intro.html might be >>> good for you. >>> >>>> >>>> public class PrintJavaClass // PrintJavaClass is public, it should be >>>> declared in a file named PrintJavaClass.java >>> >>> Yes. Public classes should be in the same filenames, so your program >>> should be written in org\jrubycookbook\ch01\PrintJavaClass.java. You >>> can found a lot of information by googling since this is a basic part >>> of Java. >>> >>>> >>>> List<Class> interfaces = Arrays.asList(o.getClass().getInterfaces()); // >>>> Incompatible types required java.util.List<java.lang.Class> found >>>> java.util.List<java.lang.Class<?>> >>> >>> I'm not sure why you got this error. The error might be resolved if >>> you could set up your Java project correctly on NetBeans. >>> >>> -Yoko >>> >>>> >>>> >>>> What environment where you using where you didn't get these errors? >>>> >>>> >>>> Carl Graff wrote: >>>>> >>>>> Actually I did find instances of the jruby.jar but I think for some >>>>> reason >>>>> these are not considered path of the default classpath >>>>> >>>>> C:\Program Files\NetBeans >>>>> 6.7\ruby2\jruby-1.2.0\lib\jruby.jar\org\jruby\Ruby.class >>>>> C:\Program Files\NetBeans >>>>> 6.5\ruby2\jruby-1.1.6\lib\jruby.jar\org\jruby\ >>>>> Ruby.class >>>>> >>>>> Carl Graff wrote: >>>> >>>>>> Hi, >>>>>> >>>>>> Unfortunately I only got to example 1.10 before getting stuck. I have >>>>>> used both Ruby and Java for several years but it is a little >>>>>> disheartening to get bogged down this early in the book. >>>>>> >>>>>> The sample code is below and here are some of my questions about it: >>>>>> 1. Most importantly is is choking on >>>>>> import org.jruby.Ruby; >>>>>> I found a jruby.Jar on the JRuby site but I am using a complete >>>>>> Netbeans >>>>>> 6.7 download (Java and Ruby) and also tried adding Ruby and JRuby >>>>>> plugins >>>>>> to a 6.5 build. Nether of these builds know what to do with >>>>>> import org.jruby.Ruby; >>>>>> Do I need to download JRuby code - in addition - to the JRuby code and >>>>>> libraries provided by NetBeans? If so do I need to put this code in a >>>>>> special directory or add an environment setting or a netbeans option >>>>>> so >>>>>> it get recognized? >>>>>> >>>>>> >>>>>> 2. The statement >>>>>> package org.jrubycookbook.ch01; Leads me to believe that either I >>>>>> need >>>>>> to download some associated sample programs or libraries or the I need >>>>>> to >>>>>> put this code in a specific directory relative to netbeans or perhaps >>>>>> relative to a separate installation of JRuby if that is needed - is >>>>>> either of these statements correct? If I need to download sample code >>>>>> or >>>>>> libraries the book does not seem to have a link to a download area. >>>>>> >>>>>> 3. The text preceding the sample indicates >>>>>> Ruby Array objects can also be coerced into Java Array objects by >>>>>> calling >>>>>> the to_java method. Example 1-10 >>>>>> <javascript:moveTo('ruby_to_java_type_conversion');> includes a >>>>>> combination of Java and Ruby code, which demonstrates this >>>>>> functionality. >>>>>> But I see no call to "to_java" in the sample code. >>>>>> >>>>>> package org.jrubycookbook.ch01; >>>>>> >>>>>> import java.io.PrintWriter; >>>>>> import java.io.StringWriter; >>>>>> import java.util.Arrays; >>>>>> import java.util.Collections; >>>>>> import java.util.List; >>>>>> >>>>>> import org.jruby.Ruby; >>>>>> import org.jruby.javasupport.JavaEmbedUtils; >>>>>> >>>>>> public class PrintJavaClass { >>>>>> >>>>>> // Output the class and interface list for a single object >>>>>> public String output(Object o) { >>>>>> String className = o.getClass().getName(); >>>>>> List<Class> interfaces = >>>>>> Arrays.asList(o.getClass().getInterfaces()); >>>>>> >>>>>> return String.format("%s, implements %s\n", className, >>>>>> interfaces); >>>>>> } >>>>>> >>>>>> // Output the class and interface list for each object in an array >>>>>> public String output(Object[] objects) { >>>>>> PrintWriter writer = new PrintWriter(new StringWriter()); >>>>>> for (Object o : objects) { >>>>>> String className = o.getClass().getName(); >>>>>> List<Class> interfaces = Arrays >>>>>> .asList(o.getClass().getInterfaces()); >>>>>> >>>>>> writer.printf("%s (inside array), implements %s\n", >>>>>> className, >>>>>> interfaces); >>>>>> } >>>>>> return writer.toString(); >>>>>> } >>>>>> >>>>>> public static void main(String[] args) { >>>>>> Ruby runtime = >>>>>> JavaEmbedUtils.initialize(Collections.EMPTY_LIST); >>>>>> String script = "@printer = >>>>>> org.jrubycookbook.ch01.PrintJavaClass.new\n" >>>>>> + "def output(o)\n" >>>>>> + "puts \"#{o.to_s} - #{@printer.output(o)}\"\n" >>>>>> + "end\n" >>>>>> + "output(1)\n" >>>>>> + "output(0.5)\n" >>>>>> + "output('string')\n" >>>>>> + "output(true)\n" >>>>>> + "output([4, 8, 15, 16, 23, 42])\n" >>>>>> + "output([4, 8, 15, 16, 23, 42].to_java)\n" >>>>>> + "output({ 'NY' => 'New York', 'MA' => >>>>>> 'Massachusetts'})\n"; >>>>>> >>>>>> runtime.evalScriptlet(script); >>>>>> JavaEmbedUtils.terminate(runtime); >>>>>> } >>>>>> } >>>> >>>> >>>> >>>> -- >>>> View this message in context: >>>> http://www.nabble.com/JRuby-Cookbook-example-1.10-tp24263046p24263046.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 >>>> >>>> >>>> >>> >>> --------------------------------------------------------------------- >>> To unsubscribe from this list, please visit: >>> >>> http://xircles.codehaus.org/manage_email >>> >>> >>> >>> >> >> > > -- > View this message in context: http://www.nabble.com/JRuby-Cookbook-example-1.10-tp24263046p24294966.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 > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Free embeddable forum powered by Nabble | Forum Help |