NullPointerExpection on basic EJB application in NetBeans 7.3.1 - maven

I'm doing a course on Enterprise computing using Beginning Java EE 6 Platform with GlassFish 3: From Novice to Professional
Last chapter was about EJB and I've actually found it very hard to understand.
I've been trying to run one of the book's sample codes in order to try to better understand EJB but I'm getting a NullPointerException whenever I call for the EJB.
I'm using NetBeans 7.3.1 and the Maven and Glassfish versions provided with NetBeans (Maven seems to be 3.0.5, and Glassfish is 4.0)
This is the code for the main class:
package org.beginningee6.book.chapter06;
import javax.ejb.EJB;
/**
* #author Antonio Goncalves
* APress Book - Beginning Java EE 6 with Glassfish
* --
*/
public class Main {
// ======================================
// = Attributes =
// ======================================
#EJB
private static BookEJBRemote bookEJB;
// ======================================
// = Public Methods =
// ======================================
public static void main(String[] args) {
// Creates an instance of book
Book book = new Book();
book.setTitle("The Hitchhiker's Guide to the Galaxy");
book.setPrice(12.5F);
book.setDescription("Science fiction comedy series created by Douglas Adams.");
book.setIsbn("1-84023-742-2");
book.setNbOfPage(354);
book.setIllustrations(false);
book = bookEJB.createBook(book);
System.out.println("### Book created : " + book);
book.setTitle("H2G2");
book = bookEJB.updateBook(book);
System.out.println("### Book updated : " + book);
System.out.println("Execution succeeded");
}
}
Here is the persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="chapter06PU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!--<jta-data-source>jdbc/__default</jta-data-source>-->
<jta-data-source>jdbc/chapter06DS</jta-data-source>
<class>org.beginningee6.book.chapter06.Book</class>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
And this is the pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.beginningee6.book</groupId>
<artifactId>chapter06</artifactId>
<packaging>jar</packaging>
<version>2.0</version>
<name>Week5</name>
<parent>
<groupId>org.beginningee6.book</groupId>
<artifactId>chapters</artifactId>
<version>2.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>${javax.persistence-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>${glassfish-version}</version>
</dependency>
</dependencies>
<!--To avoid multiple modules with Maven, here is what you need to manually do (it's not nice, but it works)
1) Comment the following section (maven-jar-plugin), package the jar, and deploy to GlassFish
2) Uncomment the following section, package the jar and run the Main class with app client -->
<build>
<!-- <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${plugin-jar-version}</version>
<configuration>
<archive>
<manifest>
<mainClass>org.beginningee6.book.chapter06.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>-->
</build>
</project>
I'm getting this error:
Exception in thread "main" java.lang.NullPointerException
at this line:
book = bookEJB.updateBook(book);
Apparently Maven is giving this error:
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project chapter06: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project chapter06: Command execution failed.
And after activating Maven's debugging option the last command line before the error is:
Executing command line: C:\Program Files (x86)\Java\jdk1.7.0_21\bin\java.exe -classpath C:\Users\Manuel_Laptop\Desktop\Week5\COIT20227LabSolWeek5\Week5\target\classes;C:\Users\Manuel_Laptop\.m2\repository\org\eclipse\persistence\javax.persistence\2.0.0\javax.persistence-2.0.0.jar;C:\Users\Manuel_Laptop\.m2\repository\org\eclipse\persistence\eclipselink\2.0.1\eclipselink-2.0.1.jar;C:\Users\Manuel_Laptop\.m2\repository\org\glassfish\extras\glassfish-embedded-all\3.0.1-b19\glassfish-embedded-all-3.0.1-b19.jar org.beginningee6.book.chapter06.Main
After going through some course tips and some forums I went and set my JAVA_HOME and M2_HOME to their respective directories but nothing worked. I even found a link to someone having an issue on the same chapter of the textbook (https://getsatisfaction.com/javaee6/topics/yet_another_chapter_6_ejb_problem) after seeing it referenced here in StackOverflow, but couldn't find anything there (didn't understand it to be honest)

It's not maven issue, you should set up ejb container in java SE enviroment to use EJB.
Try someithing like this
EJBContainer.createEJBContainer()
you can get complete example at http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/embeddedContainerDemo/EmbeddedContainerDemo.html

Related

Building SOA composite application using maven

Using SOA suite 11, trying building source code (composite.xml with configuration file) in SOA composite application into a jar file using maven.
Can anyone help to guide me making POM.xml for the same. i am using "Apache-ANT-Plugin" in my pom file.
http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
This POM relates to this SOA Composite, i.e. the one in this same directory.
There is another POM in the SOA Application directory which handles
the whole SOA Application, which may contain additional projects.
-->
4.0.0
test1
HelloWrold
1.0-SNAPSHOT
sar
<!--
The parent points to the common SOA parent POM. That is a special POM that is
shipped by Oracle as a point of customization (only). You can add default values
for properties like serverUrl, etc. to the SOA common parent POM, so that you
do not have to specify them over and over in every project POM.
-->
<parent>
<groupId>com.oracle.soa</groupId>
<artifactId>sar-common</artifactId>
<version>12.1.3-0-0</version>
</parent>
<properties>
<!-- These parameters are used by the compile goal -->
<scac.input.dir>${project.basedir}\SOA/</scac.input.dir>
<scac.output.dir>${project.basedir}/target</scac.output.dir>
<scac.input>${scac.input.dir}/composite.xml</scac.input>
<scac.output>${scac.output.dir}/out.xml</scac.output>
<scac.error>${scac.output.dir}/error.txt</scac.error>
<scac.displayLevel>1</scac.displayLevel>
<!-- if you are using a config plan, uncomment the following line and update to point
to your config plan -->
<!--<configplan>${scac.input.dir}/configplan.xml</configplan>-->
<!-- These parameters are used by the deploy and undeploy goals -->
<composite.name>${project.artifactId}</composite.name>
<composite.revision>1.0</composite.revision>
<composite.partition>default</composite.partition>
<serverUrl>${oracleServerUrl}</serverUrl>
<user>${oracleUsername}</user>
<password>${oraclePassword}</password>
<overwrite>true</overwrite>
<forceDefault>true</forceDefault>
<regenerateRulebase>false</regenerateRulebase>
<keepInstancesOnRedeploy>false</keepInstancesOnRedeploy>
<!-- These parameters are used by the test goal
if you are using the sca-test (test) goal, you need to uncomment the following
line and point it to your jndi.properties file. -->
<!--<jndi.properties.input>UNDEFINED</jndi.properties.input>-->
<scatest.result>${scac.output.dir}/testResult</scatest.result>
<!-- input is the name of the composite to run test suties against -->
<input>${project.artifactId}</input>
<!--<scac.ant.buildfile>${env.MW_HOME}/soa/bin/ant-sca-compile.xml</scac.ant.buildfile>
<sca.ant.testfile>${env.MW_HOME}/soa/bin/ant-sca-test.xml</sca.ant.testfile>
-->
</properties>
<build>
<plugins>
<plugin>
<groupId>com.oracle.soa.plugin</groupId>
<artifactId>oracle-soa-plugin</artifactId>
<version>12.1.3-0-0</version>
<configuration>
<compositeName>${project.artifactId}</compositeName>
<composite>${scac.input}</composite>
<sarLocation>${scac.output.dir}/sca_${project.artifactId}_rev${composite.revision}.jar</sarLocation>
<serverUrl>${serverUrl}</serverUrl>
<user>${user}</user>
<password>${password}</password>
<!-- Note: compositeRevision is needed to package, revision is needed to undeploy -->
<compositeRevision>${composite.revision}</compositeRevision>
<revision>${composite.revision}</revision>
<scacInputDir>${scac.input.dir}</scacInputDir>
<input>${input}</input>
</configuration>
<!-- extensions=true is needed to use the custom sar packaging type -->
<extensions>true</extensions>
</plugin>
</plugins>
</build>

display application version in title using thymeleaf and springboot

I want to display in my htm page the version of my webapp, using something like this (thymeleaf inside) :
<h4 th:text="${version}">Version</h4>
The data is well set in the pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.test.navig</groupId>
<artifactId>navigo</artifactId>
<version>2.0.3-SNAPSHOT</version>
...
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Application</mainClass>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
and I can see it in the MANIFEST.MF (which is in the generated jar under META-INF) :
Implementation-Version: 2.0.3-SNAPSHOT
I've tried to get the appplication version in the controller and set it in a ModuleAttribute :
#ModelAttribute("version")
public String getVersion() {
logger.info("ModelAttribute to get application version");
return getClass().getPackage().getImplementationVersion();
}
But getClass().getPackage().getImplementationVersion() value is null. Indeed the package implementationVersion is not the implementation Version of the application by default.
I know I'm late but Patrick's answer and Spring docs greatly helps in this matter.
1. If your pom.xml use spring-boot-starter-parent as parent, you can use #project.version# to get version (and any other Maven properties) in your application.properties file. According to Spring docs:
You can automatically expand properties from the Maven project using
resource filtering. If you use the spring-boot-starter-parent you
can
then refer to your Maven ‘project properties’ via #..# placeholders
Maven pom.xml:
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Foo</name>
<description>Bar</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Spring application.properties:
foo.app.version=#project.version#
2. Then a class annotated with #ControllerAdvice can be used to inject version as model attribute.
#ControllerAdvice
public class ControllerAdvice {
#Value("${foo.app.version}")
private String applicationVersion;
#ModelAttribute("applicationVersion")
public String getApplicationVersion() {
return applicationVersion;
}
}
3. Finally this model attribute can be accessed by Thymeleaf as any other.
<th:block th:text="${applicationVersion}"></th:block>
Hope this helps!
Here is the simplest way I've found :
In my controller :
#ModelAttribute("version")
public String getVersion() throws IOException {
logger.info("ModelAttribute to get application version");
Manifest manif = new Manifest(
Application.class.getResourceAsStream("/META-INF/MANIFEST.MF"));
String version = (String) manif.getMainAttributes().get(
Attributes.Name.IMPLEMENTATION_VERSION);
return version;
}
In my htm page :
<h4 th:text="${version}">Version</h4>
You need to configure resource plugin to activate filtering on the file that need to be enriched with properties coming from your POM file.
In the generated war, the version (in fact ${project.version}) will be hardcoded to your POM version.

scala Ide Not identifying my class as main method

I have researched some answers in Stackoverflow but my error seems to be different. Having said that i am a newbie in scala hence please consider that as well.
I am trying to create multi class project in scala. Project Format is like this
Test Project
|
COM Package
|
|-->App.class
|
COM.Test Package
|
|-->App1.class
Code Snippet
App.scala
object App {
def main(args : Array[String]): Unit = {
var logger = Logger.getLogger(this.getClass())
if (args.length < 3) {
logger.error("=> wrong parameters number")
System.err.println("Usage: MainExample <path-to-files> <srcCode> <tableName>")
System.exit(1)
}
println("In Main Class")
}
App1.scala
object App1 {
def main(args : Array[String]): Unit = {
println("In Sub Class")
}
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<name>${project.artifactId}</name>
<description>My wonderfull scala app</description>
<inceptionYear>2015</inceptionYear>
<licenses>
<license>
<name>My License</name>
<url>http://....</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.10.4</scala.version>
<scala.compat.version>2.10.4</scala.compat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<!-- >testSourceDirectory>src/test/scala</testSourceDirectory -->
<!-- plugins>
<plugin>
<see http://davidb.github.com/scala-maven-plugin >
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<args>
<arg>-make:transitive</arg>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
</plugins -->
</build>
</project>
I am using SCALA IDE along with Scala Maven Plugin. I have commented out the build plugin but it doen=s not make a difference if that is turned on as well.
Two Issues i am facing
Issue 1) I am unable to Run App1.scala in Sub Package COM.Test. App.scala is run without issue from Run As -->Scala Application. But when ever i try to run App1.scala the scala side is not able to find Main Class
Issue 2) I am unable to debug my code for App.scala in scala ide. Whenever i run Debug Command (Bug Operator) the breakpoints are skipped and code executes entirely. I use Scala JVM Launcher and checked The option Stop in Main. Please help
Using Eclipse Luna with Scala IDE and MAC OS
Package structure must correspond to directory structure in the IDE. (This is unlike scalac.)
It's hard to tell from the info you present, but I can confirm that Scala IDE (on Eclipse Luna) will not offer "Run As > Scala application" if I remove the package declaration.
For example, if the class App in directory src/p/ (source file src/p/App.scala) is not in package p, then it won't offer to run it.
Uncommenting the correct package declaration gets the run as menu back.
I can also verify that a debug config with stop in main and Scala JVM launcher works as expected for App in the default package as well as in a named package. So I don't know how that could have broken for you.
You need to add extends App to your main objects.
object App extends App {
def main(args : Array[String]): Unit = {
var logger = Logger.getLogger(this.getClass())
if (args.length < 3) {
logger.error("=> wrong parameters number")
System.err.println("Usage: MainExample <path-to-files> <srcCode> <tableName>")
System.exit(1)
}
println("In Main Class")
}

Causes for java.io.FileNotFoundException: class path resource [services.xml] cannot be opened because it does not exist

I have a web-application on RAD 7.5. I am building the application using Maven and deploying on WebSphere 6.1.
A part of my web.xml is:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:services.xml</param-value>
</context-param>
This is the only place where I am referencing services.xml.
My services.xml is in folder
FruitApplication/src/main/resources/services.xml
After building with maven, services.xml is in the following path in the target folder
target/FruitApplication-1.0.0/WEB-INF/classes/services.xml
On deployment, I continuously get below mentioned error. However, on building the application two three times, it disappears on its own and then reappears again haphazardly.
What is the actual cause of this error ?
How to solve this error ?
The top of my services.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
Is something needs to be changed here ???
Below is the complete pom.xml I am using for "maven-install":
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>FruitApplication</groupId>
<artifactId>FruitApplication</artifactId>
<packaging>war</packaging>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<archive>
<manifestFile>
src/main/webapp/META-INF/MANIFEST.MF
</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
... dependencies here ...
</dependencies>
</project>
I have already tried all the below three options:
<param-value>classpath:services.xml</param-value>
<param-value>classpath:/services.xml</param-value>
<param-value>WEB-INF/classes/services.xml</param-value>
How I am deploying:
I am removing the application from the server by right clicking on server and "add or remove applications"
I am doing a run-as "maven-clean" and then run-as "maven-install" on the application and again adding the application by right clicking on the server and "add or remove applications"
Did you try classpath:/services.xml? The leading slash means to pull the resource from the root of the classpath. Without it, the file is expected to be in the package of whatever class is loading it (which I admit I don't know what that would be in this case).
See http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream%28java.lang.String%29
Select project where the reource files exist.Right click - Build path - configure build path - sources -Add folder - add the folder where you resource file exist.This would add the folder to you class path.
If the above project/jar is part of another WAR/EAR then select that WAR and/or EAR - properties - j2ee module dependecies and select the above project to get added to build path.
Restart the server.
Figure out if its Websphere/RAD or a application issue?
First build the war file and drop it in a Tomcat container and check if you still see the error.
1. If you see then copy the services.xml to WEB-INF and WEB-INF/classes directory and see if it works.
2. Also check your CLASSPATH system environment variable, system CLASSPATH can override your application classpath.
If everything is fine with Tomcat, there is something specific to Websphere/RAD:
3. Check you websphere environment, I recall there is some option in websphere where you can set/override/prefix the classpath.

How to access the internal sun.security class from an OSGI bundle?

What options do I need to add to the maven build or the java runtime to access the internal sun.security classes? There is Java code from Akamai in an OSGI bundle needs access to internal sun.security classes. The Apache Felix console gives errors for the OSGI bundle:
sun.awt.image.codec -- Cannot be resolved
sun.io -- Cannot be resolved
sun.misc -- Cannot be resolved
sun.rmi.rmic -- Cannot be resolved
sun.security.action -- Cannot be resolved
sun.security.ec -- Cannot be resolved
sun.security.internal.interfaces -- Cannot be resolved
...
I looked at this article about using internal sun classes but it only refers to javac. My maven build starts like:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
<modelVersion>4.0.0</modelVersion>
<artifactId>cdncache</artifactId>
<packaging>bundle</packaging>
<name>NCDN Cache</name>
<description>Classes and interfaces to expire resource from the Akamai CDN cache [build:${build.number}]\
</description>
<version>1.0-${build.number}</version>
<properties>
<!-- Skip tests, so maven execution is faster. -->
<maven.test.skip>true</maven.test.skip>
<file.encoding>utf-8</file.encoding>
</properties>
<build>
<plugins>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.0.1</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
com.nymag.akamai,
com.akamai.*,
...
</Export-Package>
<Private-Package>
org.apache.axis.*,
...
sun.security,
sun.security.ec,
</Private-Package>
<Bundle-Version>1.0</Bundle-Version>
<Bundle-Activator>com.nymag.akamai.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
...
I agree with stjohnroe that using VM-specific classes is usually bad, but sometimes you have to (for instance, as you are currently in a transition phase). If you want to do so, you can add
org.osgi.framework.system.packages.extra=sun.your.package.of.choice
to the framework properties. If you use the standard Felix launcher, you can edit conf/config.properties for that.
All of these are non public API classes and cannot be relied upon to be present in all jre distributions. I believe that they are all present sun distributions, but not in IBM distributions etc. Try running against a Sun distribution, but this looks like a case of building against undocumented features, a big no no.

Resources