Maven J2EE Eclipse: Hello world in J2EE and Maven on Eclipse

Step by step tutorial on J2EE and Maven on IDE Eclipse

Here is a simple yet basic tutorial for a J2EE application using maven and Eclipse from the scratch showing you step by step.

This tutorial assumes you know the basics of Maven and J2EE. This is just a step by step tutorial to show the marriage of the two.

Also, it is assumed that you have installed maven and tomcat on your machine which are fairly easy to do with simple googling.

Lets Start

1. Create simple Hello world module on Eclipse
1.a Open Eclipse Go to file->new->Dynamic Web Project
1.b Name the project as MavenEclipseJ2EE and hit Finish

We have finished the first step.

Now, maven expects some kind of folder structure. Well, you can do it in your own way and make extra configuration, or you can accept what is accepted as standard and have fun.. I go with the standard maven directory structure.

Here is the maven structure


src/main/java
src/main/resource
src/main/webapp

These are going to the source folders. So lets create them.

2. Creating source folders that are acceptable by maven

2.a Go to terminal or how ever you would create folders and create the folders as:

MavenEclipseJ2EE/src$ mkdir main
MavenEclipseJ2EE/src$ mkdir main/java
MavenEclipseJ2EE/src$ mkdir main/resources
MavenEclipseJ2EE/src$ mkdir main/webapp

2.b Go back to Eclipse, select the project and hit F5 or right click on Project and hit refresh. You will see the newly created folders under src

2.c Right click on the project, select properties, select build path and hit source tab

2.d You would see the previous source folder there. Select it and hit remove to remove it. We do this since we are changing the source folders

2.e Then hit the button add folder, expand src, expand main and select the java, resources and webapps only. Hit OK and hit OK again to finish creating source files.

Cool, so our folder structure is ready for Maven. Now lets move to the maven itself.

Maven relies highly on the pom.xml file that we would put on the project root folder. Lets do that.

Continue to part two

The BASEDIR environment variable is not defined correctly This environment variable is needed to run this program

BASEDIR environment not define error and its solution

Are you working on J2EE with Tomcat?

Trying to run Tomcat from the command line and got this problem?

I got this while working on apache-tomcat 7.

Here is how I solved it.

The basedir environment will complain if you have either the JAVA_HOME and/or the CATALINA_HOME paths are not correctly set on the environment variables.

where to get the environment variable? -> in /etc/environment.
Once you make sure paths are set correctly, the next would be allowing the shell scripts to have the executable permissions.

Basically I would do
sudo chmod 777 -R /PATH/TO/TOMCAT/bin

This, as you know is not advisable if for production or networked environments as it will allow all to execute.

Now, you should be able to startup tomcat as
./startup.sh by going into the tomcat’s bin folder.

Recommended

What is can not access member of class with modifiers and its solution?

Change port in tomcat

See what the no persistence provider error mean for entity manager and how to solve it

Cannot send session cache limiter – headers already sent Error in php Session

Well, Session have both plus and minus parts, but we do use them largely in PHP. So, while developing have you come across Cannot send session cache limiter – headers already sent error but you can’t find any echoed part on the given file? I have got this problem and found out that there is a left over space at the end of the file.
So check if you have spaces either at the beginning or at the end of your file and remove that.

re-structre

Change default source directory src/java in eclipse for java project

Prepare directory structure for maven by changing the default src

Probably you are using maven and wanted to change the directory structure.

Since maven recommends the standard src/main/java src/main/resource you might want to change it that way

Here is how to do it in eclipse.

First create the folder structure on the project in which ever way you would want to do it. I would use simple command like mkdir folderName to create it.

1. right click on the project, and hit refresh, make sure the folders you created are listed there.

2. Right click on the project, select properties and select java build path

3. Go to source tab

4. select and remove the current source folder, by default it would be src folder

5. Hit the Add newthen select your folder structure there.

maven directory structure

maven directory structure

This would change the original structure of the your java project from src to the one maven compatible.

See how to use maven to create starter boilerplate application
Get started with step by step j2ee and maven tutorial using eclipse

Can you solve these cool algorithm problems? See their solution as well

How do you find the maximum consecutive sum from the given array

You are given billion numbers and to look for a couple of missing numbers

From list of numbers in array, find those that are complementary to the given number

Using If else conditional statement on where condition mysql

Using if / else on the mysql would be much easier to express with simple query. Her we go:

Assume you have the following table:

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| file_id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| file_path    | text         | NO   |     | NULL    |                |
| file_name    | varchar(120) | NO   | MUL | NULL    |                |
| file_size    | bigint(20)   | YES  |     | NULL    |                |
| file_content | text         | NO   | MUL | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

Then you want to collect records either by file_id or by file_size based on the file_size, if the file size above some value, check the name of the file to contain xls, otherwise, consider only files that have file id more than y

select * from file where if (file_size > x, file_name like '%.xls', file_id > y)

YUP!

Render hidden element with out decorator in Zend Framework

When you add hidden element on the zend form, you would see the hidden elements taking up places wrapped by dt dd tags.
To get rid of that and view only the hidden element, do:
$form->addElement(‘hidden’, ‘somename’, array(‘decorators’=>’ViewHelper’)); – this would render it without the decorators.

Regex for individual words in php

This is a simple check for words that you might want to check if the given string is this word or that word. Possible utilization could be: when you allow the user to upload file, you might want to get only certain type of file only like in the case of image jpeg, jpg, and gif only

if (preg_match('/jpeg|jpg|gif/i')){
   //do uploading
}else{
    $message = "supported file types are ...";
}
patching

Applying and using patch diff in mercurial

Applying/Commiting mercurial patches

You want to apply the changes you have to a repository or another branch
So, basically you want to apply the patch

See how to list all the files from given revision

Here are step by step procedures

Create the patch first. Being on your current repository do:

hg diff > mychanges.diff

BTW, you can achieve this though hg export also.

– Clone a new repository or move to the repo you want to apply the patch.

– move to your repository and apply the patch

hg import mychanges.diff

Using the above, you can apply the patch to mercurial and also see the applied path to the repository.

Also if you are on mercurial see:

How to list files in the revision

Solve the error python headears are required to mercurial

How to get the diff of two branches in mercurial

Do you like solving algorithms and improve your problem solving skills?

Given billion sequential but randomly listed numbers find the missing ones

Find the first occurrence of the number

Piping ls to cp in unix

How to copy the result of ls to directory

Actually this would be a generic implementation. But, as an illustration, if you want to pass the result of ls [list command] to copy, cp, command in unix you can do


ls /some/directory | xargs -i cp {} /new/copy/directory

Just piping the list to cp so that cp can you the listed values to copy.
As you can see this can be implemented to a lot more commands like mv, rm …

Say if you want to move the result of ls to new destination


ls /some/directory | xargs -i mv {} /new/copy/directory

How would you solve the following algorithms?

From billion numbers, find missing numbers with limited memory

Finding K complementary numbers from the given array

outstanding uncommitted changes hg Mercurial Error

If you try to push the changes you have to the repository, you might come across outstanding uncommitted changes error in mercurial

It would happen when you have some changes pending in your local repository and you want to pull and rebase from the repository so that you can commit the changes you have

but, you might not want to push the changes even to your local repo.

here is the fix for it:
lets say the module you have is testHG which you cloned it from http://sometestHg/hg
1. Do hg clone testHG testHG_temp – create the clone of the current module, not copy!!
2. cd testHG_temp – move to your new clone
3. hg pull –rebase http://sometestHg/hg – note you have to actually go to the main repo
4. hg push http://sometestHg/hg

Then you can delete the temp directory you created
Happy mercuriying!!