JavaOne 2007 Demo Script

There were 2 NetBeans+Ruby demos at JavaOne this year; one in Bob Brewin's technical keynote demo Tuesday afternoon, and one in James Gosling's session Friday morning.

The following demo script is the Friday morning demo (which is longer). You can view the video here .

(I started documenting the other demo, which added speech to the Mephisto blogging application, here, but it is not complete.)


  • Configure a database containing a table named "Currency", containing three columns - "Country", "Currency", "Name" and Populate the table with some data, such as "USA, Dollar, USD", "Czech Republic, Koruna", "CZK", "Norway, Krone, NOK", etc. If you're using JavaDB (Derby), you can just run the attached SQL Script, CURRENCY_JavaDB_RubyJavaOne2007Demo.sql. No offense if I excluded your country - it's a wiki, so feel free to add it :-).
  • Configure NetBeans to be able to access the table (e.g. configure a JDBC driver if necessary and add a data source in the Runtime window)
  • Create a Ruby On Rails project. Open its properties window, choose "Java" and click on Add Jar. Point to "toplink-essentials-agent.jar" and
 "toplink-essentials.jar" which can be found in your NetBeans installation's java1/modules/ext/toplink/ directory. You don't have to
 edit Rails' config/database.yml in this project since our Rails application will be having the Java code do all its database lookup.
  • Show the Welcome page
  • Add two code templates to the Java editor:
  • "jpaq"
${Query default="Query" editable="false" type="javax.persistence.Query"} ${query newVarName} = ${em instanceof="javax.persistence.EntityManager"}.createQuery("select ${p} from ${table} ${p}");
  • "jpaem":
${EntityManager default="EntityManager" editable="false" type="javax.persistence.EntityManager"} ${entityManager newVarName} =
            ${Persistence default="Persistence" editable="false" type="javax.persistence.Persistence"}.createEntityManagerFactory("${persistenceUnit}").createEntityManager();

These two should be included in the product; I've just forgotten to contribute them. Ugh will try to do that shortly.


I start out creating a Swing app - the Desktop application type with databinding. I point it at my datasource, hit a couple of buttons, and voila - a CRUD app showing my data. I run it to show the data.

  • Click the "New Project" link to create a new project.
  • Browse to category "Java", project type "Java Desktop Application". Hit Next.
  • Name the project "Money".
  • Change the type from "Basic Application" to "Database Application". Hit Next.
  • Select the Currency data source from the drop down, accept defaults and hit Ok.
  • This creates an application skeleton. Hit F6 to Run the application.
  • The window pops up showing a simple CRUD app on the currency data. Can briefly show
 the application skeleton (About box) etc. 
  • Hit Ctrl-Q to exit out of the application.

Then the editing part: I jump into the generated entity class (Currency, generated by Matisse/the project template, from the CURRENCY database table).

  • Hit Ctrl-F6 to open the tasklist (showing compilation errors, warnings, etc.)
  • Double click on the class in the explorer
  • OR, Ctrl-Shift-O and type "Curr", hit Enter.
  • Hit Ctrl-F12 to show the class' members.
  • Use this dialog to jump around in the class; e.g. to show the class
 type "Cur" and hit return to jump to the class definition.
  • Invoke code completion on the annotations to show the @Table
 annotation's documentation. 
  • Notice that NetBeans has picked reasonable defaults for th edatabase
  • Jump to the @Column annotation on say the Name field to show
 available attributes to do more sophisticated database binding.

I then state that I want to write some Java code to access these JPA entities. So I create a new class in the same package, CurrencyLookup. I will be calling CurrencyLookup from Rails to get the list of currencies.

  • Right click on the project, choose New Class
  • In the wizard enter the name "CurrencyLookup"
  • Hit OK

This part of the demo shows some code editing, and in particular, code completion and live templates.

  • Inside the class somewhere, type "pu" + Ctrl-space to complete "public",
  • Then type "List<Cu" followed by Ctrl-K to get "List<Currency"
  • Type "> getAll() {" such that you now have "List<Currency> getAll() {".
  • Pause for a second; you now get a quick tip to resolve the unknown "List" class; hit Alt-Enter and choose "java.util.List".
  • (We could alternatively have used code completion to insert List which would also have imported the class)
  • Hit Return to edit the method body (inserts a matching }).
  • Type "jpaem" followed by tab to insert an entity manager code template
  • Hit Tab to accept "em" as the entity manager name; replace the persistence unit name as "testPU.xml".
  (This may have changed since M9).
  • Type "jpaq" + tab to insert a JPA query.

At this point the "query" variable created by the second template is highlighted in red. I point out that it means the variable is unused - which is often a problem, so I've customized my IDE to show unused elements as red. (I then quickly pull up the options, which I've warmed up before the demo, and show it.)

I then instant-rename "em" to "entityManager".

  • Make sure the caret is on "em"; hit Ctrl-R to initiate in-place replacement and type entityManager.
  • Write "return query." and invoke code completion - this shows code completion filtering
 smart results to only show methods returning a List (the method's return type).
 Choose the smart result and hit ok.

At this point we've written a query class with a method with some pretty hairy code; let's test it!

  • Press Ctrl-Shift-U (to invoke Tools | Create Unit Test). Accept defaults.
  • In the generated class, navigate to the getAll method's test (Ctrl-F12)
  • Delete the dummy code
  • Write a single line: assertTrue(list.contains(new Currency(
  • At this point I see that the Currency persistence class doesn't have

a constructor allowing me to set all three properties of a currency (name, country, code) in one shot, so I jump over to it (Alt-Tab), and use code completion to insert a constructor (Ctrl-Space, choose the suggested constructor).

  • I then Alt-Tab back - my caret is still on

Currency, so I invoke Code completion again, and I now get the new constructor.

  • Select it, and the parameter matching in NetBeans 6

kicks in, so we're gradually replacing "country", "name" and "code". For my test, I insert "Brazill" (TYPO!), "BRL" and "REAL". I shift-F6 this file to run the unit test, it gets run and fails. I then correct the typo and try again - this time it succeeds.

Now it's time to access this Java code from our Rails application. During the JavaOne demo, I had pre-configured the Rails project to find the Java code based on the name of the application. There's now a UI to do this:

  • Make sure the Desktop application is built - hit Ctrl-Shift-F11 which does a full clean build
  • Right click on the Rails project
  • Choose the Java tab
  • Add Jar
  • Browse to the Money project's "dist" directory and choose Money.jar

We're now going to add currency lookup:

  • Run the Rails project. This brings up the default Rails page.
  • Edit the URL to append "/money/list" and hit Ok.
  • This gives an error message. Explain that we're now going to hook up an action to our application to respond to this URL such that it will list the available currency codes.
  • Right click on the Rails project and choose "Generate". In the name field type "money" and in the views field type "list". Hit OK.
  • After a few seconds it opens up the Rails "money" controller we just created.
  • Edit the bod of the "list" method (inside "def list, end").
  • Type "jc"+tab to insert the java-class code template.
  • Type "money.CurrencyLookup" (use Ctrl-K to quickly enter CurrencyLookup after a few characters. If it inserts CurrencyLookupTest hit Ctrl-K again.
  • Type "lookup ="
  • Notice how "lookup" is detected as unused
  • Then type in this code:
x = ""
list = lookup.getAll()
list.each { |c|
   x << + "<br>"
  • At this point (with the caret after render) hit Ctrl-Space to open code completion and show the documentation for the render method
  • Change it into
render_text x
  • Use instant rename (Ctrl-R) to rename "x" into "mytext"
  • Hit Shift-F6 to open the browser on the current action. It now lists the currency codes, one per line.

"Okay, now it works, but this is not the right way to do it" - let's build a Rails view.

  • Edit "list = lookup.getAll()" by prefixing it with "@" to end up with "@list = lookup.GetAll()" and delete the rest of the body
  • Choose "Navigate | Go to Rails/Action View" (Ctrl-Shift-A).
  • Type "table" + tab to insert a table skeleton.
  • Select the <tr> block.
  • Hit Alt-Enter to show the quick fix showing up as a lightbulb; choose "Surround with for" and hit Enter.
  • Tab through the parameters of the for block; we'll call the loop variable "c" (for currency), and we want to iterate through "@list"
  • Use code completion to add "<td></td>" inside the <tr>
  • Inside the <td> type "re" + tab to insert a code template for a Ruby expression, <%= %>.
  • Inside the <%= %> block, insert ""
  • Copy this <tr> block twice time, and edit the two other instances of "<%= %>" to <%= %> and <%= c.code %>.
  • Hit Shift-F6 to show the view in the browser - it should now be showing the table properly formatted

Finally delete some code, hit save, delete some more code, hit save - and bring up Versioning | Local History | Show Local History. Select among the versions to show previous save points in the file and how we can get back if we've really messed up.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo