blob: 9b177f544e0e60cc7dc2bc2f2911137eaf28156c [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
<meta name="description" content="Apache TomEE">
<meta name="author" content="Apache TomEE">
<meta name="google-translate-customization" content="f36a520c08f4c9-0a04e86a9c075ce9-g265f3196f697cf8f-10">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate, max-age=0">
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le styles -->
<link href="./resources/css/bootstrap.css" rel="stylesheet">
<link href="./resources/css/prettify.css" rel="stylesheet">
<!--link href="./resources/css/bootstrap-mods.css" rel="stylesheet"-->
<link href="./resources/css/main.css" rel="stylesheet">
<link href="./resources/font-awesome-4.6.3/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript">
var t = encodeURIComponent(document.title.replace(/^\s+|\s+$/g,""));
var u = encodeURIComponent(""+document.URL);
function fbshare () {
window.open(
"http://www.facebook.com/sharer/sharer.php?u="+u,
'Share on Facebook',
'width=640,height=426');
};
function gpshare () {
window.open(
"https://plus.google.com/share?url="+u,
'Share on Google+',
'width=584,height=385');
};
function twshare () {
window.open(
"https://twitter.com/intent/tweet?url="+u+"&text="+t,
'Share on Twitter',
'width=800,height=526');
};
function pinshare () {
window.open("//www.pinterest.com/pin/create/button/?url="+u+"&media=http%3A%2F%2Ftomee.apache.org%2Fresources%2Fimages%2Ffeather-logo.png&description="+t,
'Share on Pinterest',
'width=800,height=526');
};
</script>
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="./favicon.ico">
<link rel="apple-touch-icon" href="./resources/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="./resources/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="./resources/images/apple-touch-icon-114x114.png">
<script src="./resources/js/prettify.js" type="text/javascript"></script>
<script src="./resources/js/jquery-latest.js"></script>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
<script src="./resources/js/common.js"></script>
<script src="./resources/js/prettyprint.js"></script>
<!--script src="//assets.pinterest.com/js/pinit.js" type="text/javascript" async></script//-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2717626-1']);
_gaq.push(['_setDomainName', 'apache.org']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="topbar" data-dropdown="dropdown">
<div class="fill">
<div class="container">
<a class="brand" href="./index.html">Apache TomEE</a>
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
Apache
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<!-- <li><a href="./misc/whoweare.html">Who we are?</a></li> -->
<!-- <li><a href="./misc/heritage.html">Heritage</a></li> -->
<li><a href="http://www.apache.org">Apache Home</a></li>
<!-- <li><a href="./misc/resources.html">Resources</a></li> -->
<li><a href="./misc/contact.html">Contact</a></li>
<li><a href="./misc/legal.html">Legal</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li>
<li class="divider"/>
<li><a href="http://www.apache.org/security">Security</a></li>
</ul>
</li>
<li><a href="./index.html">Home</a></li>
<li><a href="./downloads.html">Downloads</a></li>
<li><a href="./documentation.html">Documentation</a></li>
<li><a href="./examples-trunk/index.html">Examples</a></li>
<li><a href="./support.html">Support</a></li>
<li><a href="./contribute.html">Contribute</a></li>
<li><a href="./security/index.html">Security</a></li>
</ul>
<!-- Google CSE Search Box Begins -->
<FORM class="pull-right" id="searchbox_010475492895890475512:_t4iqjrgx90" action="http://www.google.com/cse">
<INPUT type="hidden" name="cx" value="010475492895890475512:_t4iqjrgx90">
<INPUT type="hidden" name="cof" value="FORID:0">
<INPUT size="18" width="130" style="width:130px" name="q" type="text" placeholder="Search">
</FORM>
<!--<SCRIPT type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010475492895890475512:_t4iqjrgx90"></SCRIPT>-->
<!-- Google CSE Search Box Ends -->
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<small><a href="./index.html">Home</a></small><br>
<h1>Hello World
<div style="float: right; position: relative; bottom: -10px; ">
<a onclick="javascript:gpshare()" class="gp-share sprite" title="Share on Google+">share [gp]</a>
<a onclick="javascript:fbshare()" class="fb-share sprite" title="Share on Facebook">share [fb]</a>
<a onclick="javascript:twshare()" class="tw-share sprite" title="Share on Twitter">share [tw]</a>
<a onclick="javascript:pinshare()" class="pin-share sprite" title="Share on Pinterest">share [pin]</a>
<a data-toggle="modal" href="#edit" class="edit-page" title="Contribute to this Page">contribute</a>
</div>
</h1>
</div>
<p>This page shows the basic steps required to create, build, and run an EJB
and EJB client in its most minimum form. It does not hide steps or rely on
special build tools or IDEs and is about the most stripped down you can
get.</p>
<p><em>See the <a href="examples.html">Examples</a>
page for a full list of examples that range from [@Stateles|Simple Stateless Example]
and [@Stateful|Simple Stateful Example]
beans, to [Dependency Injection|Injection of env-entry Example]
, JDBC [DataSources|Injection of DataSource Example]
, JPA [EntityManagers|Injection of EntityManager Example]
and more.</em></p>
<p><a name="HelloWorld-AbasicEJBexample"></a></p>
<h2>A basic EJB example</h2>
<p>Here are some basic steps you need to perform to get started with OpenEJB</p>
<ol>
<li>Download and install OpenEJB</li>
<li>Setup your development environment</li>
<li>Write an EJB</li>
<li>Write an EJB client</li>
<li>Start the server</li>
<li>Deploy the EJB</li>
<li>Run the client</li>
<li>Stop the server</li>
</ol>
<p><a name="HelloWorld-DownloadandinstallOpenEJB"></a></p>
<h2>Download and install OpenEJB</h2>
<p>This example pertains to OpenEJB 3.0 which can be <a href="http://archive.apache.org/dist/openejb/3.0">downloaded here</a>
. Once you have downloaded OpenEJB, you can then simply extract the
contents of the downloaded file to whichever directory you want to install
OpenEJB in. </p>
<p>After extracting the file contents, you should now see a directory named
openejb-3.0. If you look under this directory, you will find a few more
directories:
- <em>bin</em>: Contains commands to start/stop the server (You can also do a lot
of other stuff like deploy/undeploy, but we will just talk about things
needed to get you started)
- <em>lib</em>: Contains several jar files (you only need of few of these jars in
your classpath to do EJB development)
- <em>apps</em>: Once you create your EJB's and jar them up, you can place your
jar file in this directory and start the server. The server will
automatically deploy all the EJB's contained in this JAR.
- <em>conf</em>: This directory contains all the configuration files. Although you
may not see any file except for a README.txt file right now, but after you
start the server, the required configuration files will be automatically
created. It is highly recommeded to read the README.txt file under this
directory
- <em>logs</em>: Contains log files. </p>
<p><a name="HelloWorld-Setupyourdevelopmentenvironment"></a></p>
<h2>Setup your development environment</h2>
<p><a name="HelloWorld-Createaworkingdirectory"></a></p>
<h3>Create a working directory</h3>
<p>Assuming you are in your home directory, create a directory named projects</p>
<pre><code>karan@poweredge:~$ mkdir projects
</code></pre>
<p>Go to the projects directory</p>
<pre><code>karan@poweredge:~$ cd projects
</code></pre>
<p>We will do all our work in this directory.
<a name="HelloWorld-InstallJava"></a></p>
<h3>Install Java</h3>
<p>Download and install Java (version 5 or higher). Also set it up so that you
can run the java and javac commands from any directory
<a name="HelloWorld-SetOPENEJB_HOME"></a></p>
<h3>Set OPENEJB_HOME</h3>
<p>We will setup this variable to refer to the openejb install location.</p>
<pre><code>karan@poweredge:~/projects$ export
</code></pre>
<p>OPENEJB_HOME=/home/karan/install/openejb-3.0</p>
<p><a name="HelloWorld-WriteanEJB"></a></p>
<h2>Write an EJB</h2>
<p>Whatever files you create should be placed under the projects directory
<a name="HelloWorld-CreatetheRemoteInterface"></a></p>
<h3>Create the Remote Interface</h3>
<p>Using your favorite editor, create a file named Hello.java (shown below)</p>
<pre><code>package org.acme;
import javax.ejb.Remote;
@Remote
public interface Hello{
public String sayHello();
}
</code></pre>
<p><a name="HelloWorld-CreatetheBeanClass"></a></p>
<h3>Create the Bean Class</h3>
<p>Now create a file named HelloBean.java (shown below)</p>
<pre><code>package org.acme;
import javax.ejb.Stateless;
@Stateless
public class HelloBean implements Hello{
public String sayHello(){
return "Hello World!!!!";
}
}
</code></pre>
<p><a name="HelloWorld-Compilethesourcecode"></a></p>
<h3>Compile the source code</h3>
<p>Since we have imported the javax.ejb.Stateless and javax.ejb.Remote
annotations, we need these in our classpath to compile our source code.
These annotations can be found in the $OPENEJB_HOME/lib/javaee-5.0-1.jar.
Lets compile our source (make sure you are in the projects directory)</p>
<pre><code>karan@poweredge:~/projects$ javac -cp $OPENEJB_HOME/lib/javaee-5.0-1.jar -d
</code></pre>
<p>. *.java</p>
<p>The above will compile all the .java files and also create the required
packages. You should now see a package named org under the projects
directory. All class files should be under org/acme directory.
<a name="HelloWorld-PackagetheEJB"></a></p>
<h3>Package the EJB</h3>
<p>To package the EJB into a JAR, run the following command while you are in
the projects directory</p>
<pre><code>karan@poweredge:~/projects$ jar cvf hello.jar org
</code></pre>
<p>The above command will package everything under the org directory
(including the org directory itself) into a jar file named hello.jar. Below
is the output from running the above command:</p>
<pre><code>karan@poweredge:~/projects$ jar cvf hello.jar org
added manifest
adding: org/(in = 0) (out= 0)(stored 0%)
adding: org/acme/(in = 0) (out= 0)(stored 0%)
adding: org/acme/Hello.class(in = 203) (out= 168)(deflated 17%)
adding: org/acme/HelloBean.class(in = 383) (out= 275)(deflated 28%)
</code></pre>
<p><a name="HelloWorld-WriteanEJBClient"></a></p>
<h2>Write an EJB Client</h2>
<p>Now we will write a Client class which will lookup the EJB , invoke the
sayHello() business method and print the value returned from the method.
While you are in the projects directory, create a new file named
HelloClient.java . Add the following to this file:</p>
<pre><code>package org.acme;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject;
public class HelloClient{
public static void main(String[]
</code></pre>
<p>args) throws Exception{
Properties props = new Properties();</p>
<p>props.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.RemoteInitialContextFactory");
props.put(Context.PROVIDER_URL,"ejbd://127.0.0.1:4201");
Context ctx = new InitialContext(props);
Object ref = ctx.lookup("HelloBeanRemote");
Hello h =
(Hello)PortableRemoteObject.narrow(ref,Hello.class);
String result = h.sayHello();
System.out.println(result);
}
}</p>
<p><a name="HelloWorld-CompileHelloClient.java"></a></p>
<h3>Compile HelloClient.java</h3>
<p>Run the following command:</p>
<pre><code>karan@poweredge:~/projects$ javac -d . HelloClient.java
</code></pre>
<p><a name="HelloWorld-StarttheServer"></a></p>
<h2>Start the Server</h2>
<p>Go to the OpenEJB install directory (i.e. OPENEJB_HOME) and run the
following command:</p>
<pre><code>karan@poweredge:~/install/openejb-3.0$ bin/openejb start
</code></pre>
<p>Once the Server starts, you will see an output similar to the below in your
console:</p>
<pre><code>karan@poweredge:~/install/openejb-3.0$ bin/openejb start
Apache OpenEJB 3.0 build: 20070926-12:34
http://tomee.apache.org/
OpenEJB ready.
[OPENEJB:init]
</code></pre>
<p>OpenEJB Remote Server
<em>* Starting Services *</em>
NAME IP PORT <br />
httpejbd 0.0.0.0 4204 <br />
telnet 0.0.0.0 4202 <br />
ejbd 0.0.0.0 4201 <br />
hsql 0.0.0.0 9001 <br />
admin thread 0.0.0.0 4200 <br />
-------
Ready!</p>
<p>Take out a minute to browse through the conf and logs directories. You
should now see some configuration and log files under the respective
directories.
<a name="HelloWorld-DeploytheEJB"></a></p>
<h2>Deploy the EJB</h2>
<p>We will now use the deploy command to deploy the EJB in hello.jar. While
you are in the projects directory, run the following command:</p>
<pre><code>karan@poweredge:~/projects$ $OPENEJB_HOME/bin/openejb deploy hello.jar
</code></pre>
<p>The above command should give you the following output:</p>
<pre><code>karan@poweredge:~/projects$ $OPENEJB_HOME/bin/openejb deploy hello.jar
Application deployed successfully at "hello.jar"
App(id=/home/karan/projects/hello.jar)
EjbJar(id=hello.jar, path=/home/karan/projects/hello.jar)
Ejb(ejb-name=HelloBean, id=HelloBean)
Jndi(name=HelloBeanRemote)
</code></pre>
<p>Notice how the output neatly lays out various deployment details. One thing
you might want to note from the output is the JNDI name. This is the JNDI
name we used in the client to lookup the EJB
<a name="HelloWorld-RuntheClient"></a></p>
<h2>Run the Client</h2>
<p>While you are in the projects directory, run the following command to run
the client:</p>
<pre><code>karan@poweredge:~/projects$ java -cp
</code></pre>
<p>$OPENEJB_HOME/lib/openejb-client-3.0.jar:$OPENEJB_HOME/lib/javaee-5.0-1.jar:.
org.acme.HelloClient</p>
<p>The above should give you the following output:</p>
<pre><code>Hello World!!!!
</code></pre>
<p><a name="HelloWorld-Help!,itdidn'tworkforme!!."></a></p>
<h2>Help! , it didn't work for me!!.</h2>
<p>No problem, we are here to help. Just send us an email at
users@tomee.apache.org. If possible, send us the contents of
logs/openejb.log file in the email. </p>
<p><a name="HelloWorld-Lookingformore?"></a></p>
<h1>Looking for more?</h1>
<p>More EJB 3.0 examples, sample applications, tutorials and howtos available <a href="examples.html">here</a>
.</p>
<div id="edit" class="modal hide fade in" style="display: none; ">
<div class="modal-header">
<a class="close" data-dismiss="modal">x</a>
<h3>Thank you for contributing to the documentation!</h3>
</div>
<div class="modal-body">
<h4>Any help with the documentation is greatly appreciated.</h4>
<p>All edits are reviewed before going live, so feel free to do much more than fix typos or links. If you see a page that could benefit from an entire rewrite, we'd be thrilled to review it. Don't be surprised if we like it so much we ask you for help with other pages :)</p>
<small>NOTICE: unless indicated otherwise on the pages in question, all editable content available from apache.org is presumed to be licensed under the Apache License (AL) version 2.0 and hence all submissions to apache.org treated as formal Contributions under the license terms.</small>
<!--[if gt IE 6]>
<h4>Internet Explorer Users</h4>
<p>If you are not an Apache committer, click the Yes link and enter a <i>anonymous</i> for the username and leave the password empty</p>
<![endif]-->
</div>
<div class="modal-footer">
Do you have an Apache ID?
<a href="javascript:void(location.href='https://cms.apache.org/redirect?uri='+escape(location.href))" class="btn">Yes</a>
<a href="javascript:void(location.href='https://anonymous:@cms.apache.org/redirect?uri='+escape(location.href))" class="btn">No</a>
</div>
</div>
<script src="./resources/js/bootstrap-modal.js"></script>
<footer>
<p>Copyright &copy; 1999-2016 The Apache Software Foundation, Licensed under the Apache License, Version 2.0.
Apache TomEE, TomEE, Apache, the Apache feather logo, and the Apache TomEE project logo are trademarks of The Apache Software Foundation.
All other marks mentioned may be trademarks or registered trademarks of their respective owners.</p>
</footer>
</div> <!-- /container -->
<!-- Javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="./resources/js/bootstrap-dropdown.js"></script>
</body>
</html>