[355244] EAR5.0 Java EE Module Dependencies, selecting "In Lib Dir" deletes imported utility jar from workspace and it cause a NPE.
[349741] Utility jars in EAR50/lib or EAR/ not listed in in EAR Java EE Module Dependencies
diff --git a/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html b/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html
index b35a7e4..2e54105 100644
--- a/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html
+++ b/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html
@@ -38,6 +38,8 @@
 <p>Bug <a href='https://bugs.eclipse.org/328093'>328093</a>. EAR Library Directory field should not have preceding slash</p>
 <p>Bug <a href='https://bugs.eclipse.org/328652'>328652</a>. Sample JSPs: SOAPElement or Element parameters result in compile errors in sample</p>
 <p>Bug <a href='https://bugs.eclipse.org/336295'>336295</a>. Adding a jar with main-class in MANIFEST.MF to EAR 1.4 is detected as application client</p>
+<p>Bug <a href='https://bugs.eclipse.org/349741'>349741</a>. Utility jars in EAR50/lib or EAR/ not listed in in EAR Java EE Module Dependencies</p>
+<p>Bug <a href='https://bugs.eclipse.org/355244'>355244</a>. EAR5.0 Java EE Module Dependencies, selecting "In Lib Dir" deletes imported utility jar from workspace and it cause a NPE.</p>
 
 </body>
 </html>
\ No newline at end of file
diff --git a/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties b/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties
index f8bdb93..c4e618d 100644
--- a/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties
+++ b/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties
@@ -55,6 +55,8 @@
 Bug https://bugs.eclipse.org/328093 EAR Library Directory field should not have preceding slash\n\
 Bug https://bugs.eclipse.org/328652 Sample JSPs: SOAPElement or Element parameters result in compile errors in sample\n\
 Bug https://bugs.eclipse.org/336295 Adding a jar with main-class in MANIFEST.MF to EAR 1.4 is detected as application client\n\
+Bug https://bugs.eclipse.org/349741 Utility jars in EAR50/lib or EAR/ not listed in in EAR Java EE Module Dependencies\n\
+Bug https://bugs.eclipse.org/355244 EAR5.0 Java EE Module Dependencies, selecting "In Lib Dir" deletes imported utility jar from workspace and it cause a NPE.\n\
 \n\
 
 # "copyright" property - text of the "Feature Update Copyright"
diff --git a/plugins/org.eclipse.jst.j2ee.ui/META-INF/MANIFEST.MF b/plugins/org.eclipse.jst.j2ee.ui/META-INF/MANIFEST.MF
index 54da2d9..ce71d0a 100644
--- a/plugins/org.eclipse.jst.j2ee.ui/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.jst.j2ee.ui/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %Bundle-Name.0
 Bundle-SymbolicName: org.eclipse.jst.j2ee.ui; singleton:=true
-Bundle-Version: 1.1.215.qualifier
+Bundle-Version: 1.1.216.qualifier
 Bundle-Activator: org.eclipse.jst.j2ee.internal.plugin.J2EEUIPlugin
 Bundle-Vendor: %Bundle-Vendor.0
 Bundle-Localization: plugin
diff --git a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java
index 994e733..0ab2a97 100644
--- a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java
+++ b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java
@@ -46,6 +46,7 @@
 import org.eclipse.jst.j2ee.application.internal.operations.RemoveComponentFromEnterpriseApplicationDataModelProvider;
 import org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil;
 import org.eclipse.jst.j2ee.componentcore.J2EEModuleVirtualArchiveComponent;
+import org.eclipse.jst.j2ee.componentcore.util.EARVirtualComponent;
 import org.eclipse.jst.j2ee.internal.common.J2EEVersionUtil;
 import org.eclipse.jst.j2ee.internal.common.classpath.J2EEComponentClasspathUpdater;
 import org.eclipse.jst.j2ee.internal.componentcore.JavaEEBinaryComponentHelper;
@@ -131,6 +132,8 @@
 	protected Set libsToUncheck;
 	protected Listener tableListener;
 	protected Listener labelListener;
+	
+	private final static int COMPONENT_NUM_OF_SEGMENTS = 1;
 
 	//[Bug 238264] the cached list of jars selected using 'add jar' or 'add external jars'
 	protected List<IVirtualComponent> addedJARComponents = new ArrayList<IVirtualComponent>();
@@ -1020,12 +1023,42 @@
 	}		
 		
 	private boolean isInLibDir(VirtualArchiveComponent comp) {
+		// Get the content directory
+		int contentDirSegmentCount = earComponent.getRootFolder().getUnderlyingFolder().getProjectRelativePath().segmentCount();
+		
+		// Get segments of component's IPath
 		IPath p = comp.getProjectRelativePath();
-		if (p.segmentCount() == 2)
-			return false;
-		return true;
+		String[] compPathSegs = p.segments();
+		
+		// Get segments of the library directory of the current EAR
+		String strippedLibDir = stripSeparators(libDir);
+		String[] libDirSegs = strippedLibDir.split(PATH_SEPARATOR);
+		
+		// Compare component's and library directory's segments to determine if "is in lib-dir"
+		if(contentDirSegmentCount + libDirSegs.length + COMPONENT_NUM_OF_SEGMENTS == compPathSegs.length){
+			for (int i = 0; i < libDirSegs.length; i++){
+				if (!libDirSegs[i].equals(compPathSegs[i + contentDirSegmentCount]))
+					return false;
+			}
+		}else{ return false; }
+		
+			return true;
 	}
 	
+	/**
+	 * This method removes leading and ending separators from the given string wich is supposed to
+	 * contain a path
+	 * @param dir
+	 * @return the string without leading and ending separators
+	 */
+	private String stripSeparators(String dir) {
+		if (dir.startsWith(PATH_SEPARATOR)) 
+			dir = dir.substring(1);
+		if (dir.endsWith(PATH_SEPARATOR))  
+			dir = dir.substring(0, dir.length() - 1);
+		return dir;
+	}	
+	
 	
 	public void refresh() {
 
@@ -1187,7 +1220,8 @@
 		String refedCompName;
 		int lastDotIndex;
 		String increment;
-		IVirtualReference [] existingRefs = earComponent.getReferences();
+		EARVirtualComponent earVirtualCompoent = (EARVirtualComponent) earComponent;
+		IVirtualReference [] existingRefs = earVirtualCompoent.getHardReferences();
 		for(int i=0;i<existingRefs.length;i++){
 			refedCompName = existingRefs[i].getReferencedComponent().getName();
 			
diff --git a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java
index 2c978a4..b1887a7 100644
--- a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java
+++ b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java
@@ -48,6 +48,7 @@
 public class AvailableJ2EEComponentsForEARContentProvider implements IStructuredContentProvider, ITableLabelProvider {
 	
 	final static String PATH_SEPARATOR = String.valueOf(IPath.SEPARATOR);
+	private final static int COMPONENT_NUM_OF_SEGMENTS = 1;
 	
 	private int j2eeVersion;
 	private IVirtualComponent earComponent;
@@ -144,36 +145,76 @@
 			return true;
 		
 		VirtualArchiveComponent comp = (VirtualArchiveComponent)component;
+		// First logic expression: workspace relative path will be null only if file doesn't exist or the path  has not more than one segment (is in root of workpsace)
+		// there's no point in showing it. Second logic expression: the first segment should be the current EAR, if not the component shouldn't be shown.
 		if(comp.getWorkspaceRelativePath() == null || !comp.getWorkspaceRelativePath().segment(0).equals(earComponent.getName()))
-			return true;
+			return false;
 		
 		IPath p = null;
 		try {
 			p = comp.getProjectRelativePath();
 		} catch (IllegalArgumentException e) {
-			return true;
-		}
-		if ((p == null) && (p.segmentCount() == 0))
-			return true;	
-		IContainer f  = earComponent.getRootFolder().getUnderlyingFolder();
-		String rootFolderName = f.getProjectRelativePath().segment(0);
-		if (!p.segment(0).equals(rootFolderName)) 
 			return false;
-		if (p.segmentCount() == 2)
+		}
+		
+		// If the path relative to the project is null it means that the file doesn't exist, component shouldn't be shown.
+		if (p == null)
+			return false;
+		
+		// Get the content directory
+		IContainer contentDir  = earComponent.getRootFolder().getUnderlyingFolder();
+		// Obtain the number of segments of the content directory
+		int ContentDirSegmentCount = contentDir.getProjectRelativePath().segmentCount();
+		// Sum the content directory number of segments plus the number of segments that represent the component (should be only one)
+		int numOfSegmentsToCompare = ContentDirSegmentCount + COMPONENT_NUM_OF_SEGMENTS;
+		
+		// If the EAR has a content folder, check if the project relative path starts with it, if not do not show.
+		if ((ContentDirSegmentCount > 0) && !startWithSameSegments(p, contentDir.getProjectRelativePath()))
+			return false;
+		
+		// At this point we know that the resource is inside the ear, now if it is anywhere 
+		// outside the lib dir we must show it (we'll take care of the ones that are in the lib dir later)
+		if (!startWithSameSegments(p, Path.fromOSString(contentDir.getProjectRelativePath().toString() + IPath.SEPARATOR + libDir)))
 			return true;
+		
+		// Show the element if it is in the EAR's root folder. If the EAR has a content folder, the number of segments will be "content folder segments" + "component name's segment"
+		// If the EAR does not have content folder, the number of segments will be 1 (the component name only) 
+		if (p.segmentCount() == numOfSegmentsToCompare)
+			return true;
+		
 		if (isEE5) {
+			// Obtain libDir, remove leading and ending unnecessary characters and split into segments
 			String strippedLibDir = stripSeparators(libDir);
-			String[] libDirSegs = strippedLibDir.split(PATH_SEPARATOR); 
-			if (p.segmentCount() - 2 != libDirSegs.length)
+			String[] libDirSegs = strippedLibDir.split(PATH_SEPARATOR);
+			// Verify if lib dir segments' number match
+			if (p.segmentCount() - numOfSegmentsToCompare != libDirSegs.length)
 				return false;
-			for (int i = 0; i < libDirSegs.length; i++) 
-				if (!libDirSegs[i].equals(p.segment(i + 1)))
+			// Verify if lib dir segments' values match
+			for (int i = 0; i < libDirSegs.length; i++)
+				if (!libDirSegs[i].equals(p.segment(i + ContentDirSegmentCount))) // If the EAR has no content folder, offset will be 0
 					return false;
 			return true;
 		}
 		return false;
 	}
 	
+	// This method determines if the provided IPaths start with the same segments 
+	private boolean startWithSameSegments(IPath iPath1, IPath iPath2){
+		
+		// Validate the provided paths
+		if (iPath1 == null || iPath2 == null)
+			return false;
+			
+		// Determine which one of the paths is shorter, if equal then doesn't matter. 
+		int minor = iPath1.segmentCount() > iPath2.segmentCount() ? iPath2.segmentCount() : iPath1.segmentCount();  
+		// Compares "minor" amount of segments
+		for (int i = 0; i < minor; i++){
+			if (!iPath1.segment(i).equals(iPath2.segment(i)))
+				return false;
+		}
+		return true;
+	}
+	
 	private String stripSeparators(String dir) {
 		if (dir.startsWith(PATH_SEPARATOR)) 
 			dir = dir.substring(1);
diff --git a/plugins/org.eclipse.jst.j2ee/META-INF/MANIFEST.MF b/plugins/org.eclipse.jst.j2ee/META-INF/MANIFEST.MF
index c4787fd..33707a4 100644
--- a/plugins/org.eclipse.jst.j2ee/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.jst.j2ee/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %Bundle-Name.0
 Bundle-SymbolicName: org.eclipse.jst.j2ee; singleton:=true
-Bundle-Version: 1.1.217.qualifier
+Bundle-Version: 1.1.218.qualifier
 Bundle-Activator: org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin
 Bundle-Vendor: %Bundle-Vendor.0
 Bundle-Localization: plugin
diff --git a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/J2EEUtilityJarImportAssistantOperation.java b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/J2EEUtilityJarImportAssistantOperation.java
index 641eb6a..16d8e0b 100644
--- a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/J2EEUtilityJarImportAssistantOperation.java
+++ b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/J2EEUtilityJarImportAssistantOperation.java
@@ -104,7 +104,7 @@
 
 		IVirtualComponent earcomponent = ComponentCore.createComponent(targetProject);
 
-		IVirtualComponent utilcomponent = ComponentCore.createArchiveComponent(targetProject, VirtualArchiveComponent.LIBARCHIVETYPE + IPath.SEPARATOR + utilityJarIFile.getProjectRelativePath().toString());
+		IVirtualComponent utilcomponent = ComponentCore.createArchiveComponent(targetProject, VirtualArchiveComponent.LIBARCHIVETYPE + IPath.SEPARATOR + utilityJarIFile.getFullPath().toString());
 
 		addArchiveProjectToEARDataModel.setProperty(ICreateReferenceComponentsDataModelProperties.SOURCE_COMPONENT, earcomponent);
 		addArchiveProjectToEARDataModel.setProperty(ICreateReferenceComponentsDataModelProperties.TARGET_COMPONENT_LIST, Collections.singletonList(utilcomponent));
diff --git a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/RemoveComponentFromEnterpriseApplicationOperation.java b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/RemoveComponentFromEnterpriseApplicationOperation.java
index d5bc93f..ff4c5cb 100644
--- a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/RemoveComponentFromEnterpriseApplicationOperation.java
+++ b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/RemoveComponentFromEnterpriseApplicationOperation.java
@@ -14,6 +14,7 @@
 
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IProgressMonitor;
@@ -63,6 +64,10 @@
 					for (int i = 0; i < list.size(); i++) {
 						IVirtualComponent wc = (IVirtualComponent) list.get(i);
 						IVirtualComponent moduleComponent = wc.getComponent();
+						IProject earProject = comp.getProject();
+						IProject componentProject = moduleComponent.getProject();
+						if (earProject.equals(componentProject))
+							continue;
 						if(!moduleComponent.isBinary()){
 							J2EEComponentClasspathUpdater.getInstance().queueUpdateModule(moduleComponent.getProject());
 						}
diff --git a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/componentcore/util/EARVirtualComponent.java b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/componentcore/util/EARVirtualComponent.java
index f888e4c..b1abfe7 100644
--- a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/componentcore/util/EARVirtualComponent.java
+++ b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/componentcore/util/EARVirtualComponent.java
@@ -207,6 +207,14 @@
 		cachedReferences = (IVirtualReference[]) hardReferences.toArray(new IVirtualReference[hardReferences.size()]);
 		return cachedReferences;
 	}
+	
+	
+	public IVirtualReference[] getHardReferences() {		
+		List hardReferences = getHardReferences(this);
+		return (IVirtualReference[]) hardReferences.toArray(new IVirtualReference[hardReferences.size()]);		
+	}
+
+		
 	// Returns cache if still valid or null
 	public IVirtualReference[] getCachedReferences() {
 		if (cachedReferences != null && checkIfStillValid())