r/java • • Sep 12 '15

JavaEE / TomEE - Hello World

Assuming that you already have the Java SDK installed, these are the steps to get a hello world program running in TomEE.

  1. Download / unpack Tomee.

    wget http://mirror.catn.com/pub/apache/tomee/tomee-1.7.2/apache-tomee-1.7.2-jaxrs.tar.gz tar -xvf apache-tomee-1.7.2-jaxrs.tar.gz

  2. Create folders to store your app.

    mkdir -p apache-tomee-jaxrs-1.7.2/webapps/helloworld/WEB-INF/classes cd apache-tomee-jaxrs-1.7.2/webapps/helloworld/WEB-INF/classes

  3. Create the HelloWorld.java

    vi HelloWorld.java

    import javax.ws.rs.*;

    @Path("/") public class HelloWorld {

     @GET
         public String home(){
         return "Hello World";
     }

    }

  4. Compile it.

    javac -classpath "../../../../lib/*" *.java

  5. Start the server.

    sh ../../../../bin/startup.sh

  6. Navigate to page http://localhost:8080/helloworld

0 Upvotes

24 comments sorted by

View all comments

2

u/sh0rug0ru__ Sep 12 '15 edited Sep 12 '15

Do you really want to play this dumb game?

Okay, here's a simple Spring Boot project, and we'll even start from scratch without having Maven installed:

  1. Download and install Maven.

    wget http://www.eu.apache.org/dist/maven/maven-3/3.3.3/binaries/apache-maven-3.3.3-bin.tar.gz tar xvzf apache-maven-3.3.3-bin.tar.gz

  2. Create a Spring Boot project. When prompted, choose option 1310 for a simple Spring Boot web project. There are preconfigured Spring Boot projects for JPA, AMQ, Websockets, Mongo, Redis, etc., etc. Specify groupId as boot-test, artifactId as boot-test and package as test.

    apache-maven-3.3.3/bin/mvn archetype:generate

  3. Add a controller at boot-test/src/main/java/test/jsp/HelloWorldController.java

    package test.jsp;

    import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*;

    @Controller public class HelloWorldController { @RequestMapping("/hello-world") public @ResponseBody String helloWorld() { return "Hello World!"; } }

  4. Run the project:

    cd boot-test mvn spring-boot:run

  5. Check out your app by navigating to:

    http://localhost:8080/hello-world

You could even commit this, because this is a real project and how you would actually use Spring Boot. You're not sticking classes into your app server's deployment directory for no useful purpose, demonstrating a practice that no sane person would use in the real world, all just to prove a useless point.

0

u/tonywestonuk Sep 12 '15

I put classes into my app servers deployment directory, because thats where you put classes you want to be deployed. Its only the same as putting PHP/html files in an apache HTTPD homepage folder.

You could, zip up the entire Tomee Folder, ship it somewhere, unzip and away you go.... if you wanted to....

I agree - normally you wouldn't do it this way for any real app..... But it was the way I first got going with Tomcat many years back. and using this way I was able to find out more about how servlets / jsp's work, makeing code changes, and those changes appearing a few seconds later in my browser window. A beginner will not be blinded by the complexities of installing Maven etc. They can just get up and running in next to no time.

What you are telling me is that it is so difficult to get a spring boot app working, you need a complex build tool like Maven to get , and this takes care of downloading the (imbedded) server, and setting up the complex directory structure, and you even need Maven to run it!

I proved you need none of this to get a simple JavaEE up and running from scratch. Just download the server, add a class in the right place to process the request, and that is it. Easy

2

u/sh0rug0ru__ Sep 12 '15 edited Sep 12 '15

I put classes into my app servers deployment directory, because thats where you put classes you want to be deployed.

Yes, obviously. But messing around with a deployed app in your app server's deployment directory is not the stuff that you should be teaching beginners, because there are a lot of pitfalls to that approach. Especially once you move past JSPs. No person in the modern world should be writing apps hosted in JSPs. That practice ended in the 1990s.

A beginner will not be blinded by the complexities of installing Maven etc.

Yes, as I demonstrated, Maven is very difficult to install. I'm still working on getting my eyesight back.

But it was the way I first got going with Tomcat many years back. and using this way I was able to find out more about how servlets / jsp's work

I also learned JSP this way. Not Servlets. Servlets are real classes that require you to recompile your classes yourself, using a tool. To understand packaging. That's when I learned about Ant and frameworks like Struts.

What you are telling me is that it is so difficult to get a spring boot app working, you need a complex build tool like Maven to get , and this takes care of downloading the (imbedded) server, and setting up the complex directory structure, and you even need Maven to run it!

Yes, I use tools. I'm not making fire by rubbing two sticks together, either.

I proved you need none of this to get a simple JavaEE up and running from scratch. Just download the server, add a class in the right place to process the request, and that is it. Easy

No, you proved that you have to download a tool. And run that tool. And then you get a web app running in a web browser. Your tool is Tomcat, my tool is Maven. The significance of the difference is only in your own mind.

2

u/tonywestonuk Sep 12 '15

Especially once you move past JSPs. No person in the modern world should be writing apps hosted in JSPs.

I am not writing an app hosted in a JSP. The 'Hello world' app, was a Jax-RS Webservice. No Servlet. No JSP.

0

u/sh0rug0ru__ Sep 12 '15

Then you've defeated your own point.

It's only with JSPs that you can drop source files into the deployment directory and have the app container do the work for you. That's where the "easy" ends.

Otherwise, you have to build things first in order to even get the classes to drop into the deployment directory. So in your silly example, you're exposing beginners to the complexity of running the java compiler manually from the command line.

-1

u/tonywestonuk Sep 12 '15

"It's only with JSPs that you can drop source files into the deployment directory and have the app container do the work for you."

Really.... Sorry, I wasn't aware of that. I should get rid of this example as it cant possibly work then.

1

u/sh0rug0ru__ Sep 12 '15

The example will work as presented. As long as the compiled classes are in WEB-INF/classes when you start the server.

-1

u/NimChimspky Sep 12 '15

Your guide implies classes can just be dropped in and easy peasy an updated helloworld2 got deployed.

This isn't the case.

1

u/lacosaes1 Sep 13 '15

No person in the modern world should be writing apps hosted in JSPs. That practice ended in the 1990s.

With the MVC specification I think JSP is going to resurge.