Showing posts with label Glassfish. Show all posts
Showing posts with label Glassfish. Show all posts

Sunday, October 27, 2013

Write Once Run Anywhere .... if you can

Let's see. A few weeks back my hard-drive crashed and I needed to replace it. Luckily, I had saved my data previously, among which a Java-based application with 3 tiers: JSF, jQuery, servlets, and an open-source database. I wanted thus to resurrect this application. "Piece of cake" I told to myself, Java is after all WORA (Write Once Run Anywhere).
So this is what I've done:

  1. downloaded and installed Eclipse Juno (which didn't start on double-clicking the exe, but rather using this command line: C:\eclipse\eclipse.exe -vm "C:\Program Files\Java\jre7\bin")

  2. I want to run this application on Apache Tomcat and Glassfish application servers, so after downloading and installing both, I needed to download server adapters, as shown in the following figure (see "Download additional server adapters" in the figure below)

Note that I tried first with Glassfish 4.0.

3. I created a Dynamic Web Project in Eclipse and imported the sources of my project (saved from the previous hard-drive)

4. Then, as I was building the project, I noticed missing required jar files, so I started looking for them (either jarfinder.com or findjar.com) and adding them to \WEB-INF\lib

At this point in time I thought everything should work fine. 
But first I encountered a strange run-time exception, as shown below:

5. After a bit of searching online, it turned out I needed to configure Project Facets to use Dynamic Web Project 3.0, as shown below:

Note: it's very important to select the right Project Facets, because that decides what jars will be used. Fro example, if you don't choose the JSF facet, then you need to add the JSF jars yourself; sometimes there's a good reason to do just that, e.g. the default provided jars won't do the job, so you want to use other jars.

6. As you can see, Eclipse complained about the Java version, so I needed to use the Java 7 compiler, as shown in the following figures:


7. After all this hassle, my application worked, but only with Glassfish 3.1; on 4.0 it throws an NPE; on Tomcat, no way to make it work, even though the same application previously worked ONLY on Tomcat and not on Glassfish ....

By the way, in the past I also had an issue with an application working fine on the former Oracle OAS and not working at all on Tomcat. It turned out that replacing the standard Oracle JSF libs with the ones from Mojarra helped. But you never know ....

I need to investigate this further, but now I'm happy at least I made it work on Glassfish 3.1.

Greetings from the jungle,
    Sorin


P.S. A great article about JSF 2.0 with Glassfish and Eclipse is here: http://balusc.blogspot.nl/2011/01/jsf-20-tutorial-with-eclipse-and.html.


Sunday, November 21, 2010

Debugging Java PetStore with Glassfish and Eclipse

Once I made PetStore work in Glassfish, I wanted to debug it in Eclipse.

1. I ran asadmin in my application server path:
C:\J2EE\Sun\AppServer\bin>asadmin start-domain

2. In the browser, I ran the application server Admin Console, available in my case at localhost:4848

3. I went to Application Server menu (top left), I chose JVM Settings and I enabled Debug

4. I restarted the Application Server (after enabling Debug and saving this configuration, I noticed the link "Restart needed" top left; I clicked on it, after that I ran asadmin start-domain from the console)

5. I also ran asadmin start-database to start the database for PetStore

6. I started Eclipse (I use version 3.4.1); under Run menu, I chose "Debug configurations ...."; then I chose the PetStore project (which I had created previously in Eclipse) and port 9009 (the default port where Glassfish is listening for debuggers); I also enabled "Allow termination of remote VM"

7. Just for the test, I put a breakpoint in ImageAction.java, method service; then I started the debug configuration created at bullet 6; finally, I navigated in a browser to
 http://localhost:8087/petstore/faces/catalog.jsp
and I clicked a dog under Pets; I could see my breakpoint being hit.

Sunday, September 5, 2010

Making Java PetStore work

Recently I've been struggling with making the Pet Store 2.0 application work.
So, shortly, what I've done:
-  downloaded the jar from https://blueprints.dev.java.net/petstore/
-  downloaded Glassfish v2.1.1 from https://glassfish.dev.java.net/

Then I tried to deploy and run the Pet Store by running the following commands:
- in the application server directory /bin: 
asadmin start-domain
asadmin start-database

- in the Pet Store demo directory C:\J2EE\PetStore\latest\javapetstore-2.0-ea5:
ant setup
ant run


Even though all commands were successful, the application won't start. More specifically I got the error "The requested resource () is not available" when navigating to http://localhost:8087/petstore.

So, without further ado, these are the steps I've performed to make it work.

1. in C:\J2EE\PetStore\latest\javapetstore-2.0-ea5\bp-project\build.properties I corrected the following variables:
a. javaee.home to point to my application server: c:/J2EE/Sun/AppServer
b. javaee.server.username to point to my username (the one I used when I installed Glassfish)
c. javaee.server.passwordfile to point to ${javaee.home}/samples/passwordfile

2. I created the file "passwordfile" under c:/J2EE/Sun/AppServer/samples containing a single line:
 AS_ADMIN_PASSWORD=myPassword (the password I chose when I installed Glassfish)


3. I also updated the file app-server.properties in C:\J2EE\PetStore\latest\javapetstore-2.0-ea5\bp-project with javaee.home=c:/J2EE/Sun/AppServer

At this point, I got the Pet Store demo running on the Glassfish server (after running again "ant setup" and "ant run"). However, only the first page was properly shown (http://localhost:8087/petstore/) and as soon as I navigated to "Enter the Store" (http://localhost:8087/petstore/faces/index.jsp), I saw a blank page.



Note: instead of running "ant run", you can also deploy the petstore.war from the /dist directory (or from /build) by using the Glassfish admin console:

 










By looking at the logging of the "ant setup" command I noticed that there was some issue with the username/password when trying to install the connection pool. I could also see this by inspecting the Glassfish log files (in the admin console, at http://localhost:4848/). So, I've done the following:

4. instead of relying on the ant script to install the connection pool and the data source (i.e. the ant setup command), I installed them by hand using the Glassfish admin console; see below the general properties of the connection pool:





 








Also, note the "Additional Properties":













Here I had to add the following properties: 
- Password with the value APP (I know this from the file app-server.properties, see 3),
- DatabaseName with the value petstore (I found this by googling the web)

At this point if you click Ping under the General tab, you should get the message "Ping succeeded".


5. I also installed the data source (using the Glassfish admin console), which I called jdbc/PetstoreDB (I know this from the web.xml file):













Finally, I could navigate to http://localhost:8087/petstore/faces/index.jsp and got the Pet Store demo working properly ! (of course, don't forget to start the DB server and the application server by running:
asadmin start-domain
asadmin start-database
)