File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Ant, Maven and Other Build Tools and the fly likes beginner maven question classpath and deployment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "beginner maven question classpath and deployment" Watch "beginner maven question classpath and deployment" New topic
Author

beginner maven question classpath and deployment

jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
Hi-

I am new to maven. I felt I needed to change to it because fatjar for some reason went too slow. I made a maven project and did the 2 steps with goals compile and install. My problem is that it can't find the items on the class path. I read maven in 5 minutes and it says to run it like this: C:\Users\mtdew3q\workspace\my-app>java -cp target/my-app-0.0.1-SNAPSHOT.jar com.
thirdshiftcoder.demoApp.Table_Demo

I am using eclipse with maven nature.

I get the message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widge
ts/Layout
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Layout
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: com.thirdshiftcoder.demoApp.Table_Demo. Program
will exit.

here is my .classpath file:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con"

path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/swt"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/hibernate"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/derbyjars"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

this is my 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>com.thirdshiftcoder.demoApp</groupId>
<artifactId>my-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>derbyDemo</name>
<description>hibernate and derby with maven</description>
</project>

To give you an idea of the structure of my directories/package I will do a print out:
my-app
src
main
java
com
thirdshiftcoder
demoApp

I added jars for hibernate, swt, and apache derby as libraries in my project. They are added from directories that start with c:\software for i.e. c:\software\hibernatejars or c:\software\swt to libraries in eclipse. When I select run as, with maven and compile goal, I don't get any errors. Run as with install goal seemed to work fine too. Are there any other goals I need besides compile and install?

Any cool tips would be appreciated. Maven is new to me. I think everything I need is in maven nature to deploy an app. I am just missing a trick.

When I run my other "java project" it works by outputting grid data from derbytours demo using hibernate and swt with apache derby.

One other thing of note off my c:\User\mtdew3q\.m2 there are jars for my project, and I am puzzled that I added swt as a library and it is in the repository, and maven can't see it as per the error message above. The first time I tried maven I tried to list dependencies in pom, but I later figured out that I didn't want anything downloaded because I already downloaded all the 3rdparty jars I need, so I deleted those dependencies out of pom.
thanks,
jim
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
hi-

I looked over maven some more. I checked the local repository and found that the jars were actually references to jars but were not an actual jar in kb (size). I deleted all the references to jars in the local repository. I added libraries using build path in eclipse for all my apache derby, hibernate, and swt.

The process I use to try to create the jar is:

1.) update project clean in eclipse using maven nature
2.) Run as ...compile for phase
3.) Run as ...package for phase

then I try to :

C:\Users\mtdew3q\workspace\my-app>java -cp target/my-app-0.0.1-SNAPSHOT.jar com.
thirdshiftcoder.demoApp.Table_Demo

I get the class not found error for the main class of the application.

I am not sure what is wrong!

I thought that is what is said to do in the 5 minute maven guide.

I am a bit out of my element as I don't have the maven book but I am doing a simple use case.

I hope it is something easy I am missing and an expert has a few moments to help me.

thanks again,
jim
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
hi-

One other quick note I just discovered. I had a typo in my jar that produced two different error messages.
Do you see how the change in the spelling of the jar name caused it to complain about swt on the second error message versus the first.
I am going to make a soda run and then see if I can figure this out.
thanks

C:\Users\mtdew3q\workspace\my-app>java -cp target/my-app-1.0-SNAPSHOT.jar com.th
irdshiftcoder.demoApp.Table_Demo
Exception in thread "main" java.lang.NoClassDefFoundError: com/thirdshiftcoder/d
emoApp/Table_Demo
Caused by: java.lang.ClassNotFoundException: com.thirdshiftcoder.demoApp.Table_D
emo
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: com.thirdshiftcoder.demoApp.Table_Demo. Program
will exit.

C:\Users\mtdew3q\workspace\my-app>java -cp target/my-app-0.0.1-SNAPSHOT.jar com.
thirdshiftcoder.demoApp.Table_Demo
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widge
ts/Layout
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Layout
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: com.thirdshiftcoder.demoApp.Table_Demo. Program
will exit.
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
Hi-

I am reporting some positive progress I made.

I had noticed earlier that the jars in the repository were only 1 kb. I found a link to fix that.

how to load 3rd party jars into repo in maven

So I loaded them and now I don't need to add them as libraries in my maven project. The jars now are the correct size for the file. The IDE acts like it recognizes them and I don't get a red x in eclipse letting me know it can't find imports.

So where to for now I am not sure. Still could not find the main class. At least I ruled out 3rd party jars now.

Sorry I posted so many times. I guess I really got stuck.

thanks,
jim
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

Jim,
It's good you posted as you made progress. Others new to Maven will learn based on what you tried.

You might be past this, but I'm not sure:
Maven doesn't use the .classpath, it just uses the POM. In Eclipse, you right click the project, select Maven and then Maven Update Project. Can you try right clicking the project and choosing run as > maven install? This will give you output in the console as to what Maven thinks is wrong. Then you cna post that console output here. It's sometimes helpful to get a clean compile in Maven itself before worrying about the Eclipse classpath.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64

hi-

I wish when I posted xml it would indent. Maybe I will try code tag.

Okay. I am trying to extract the jars into a directory named dependency-jars. The reason for this is here:

extract jars


When I run install I get the following error. I think my xml is not correct. line 98 is about the start of the filter which is a hack to prevent
an error. The error roughly complained that m2 can't copy to the dependency-jars dir and I have to exclude that goal to mask the error.

exclusion filter

I think the xml in the pom file isn't quite right around that big config section and that is why the error. It is just a guess.

I didn't have a manifest section before.

I am getting error resolving version for plug-in - org.apache.maven.plugins in my repo. This error is when I place cursor over red x in ide. That plug in and artifacts are indeed there.

This next and maybe related error is in the console when I do an run as install.
The project com.thirdshiftcoder.demoApp:my-app:0.0.2-SNAPSHOT (C:\Users\mtdew3q\workspace\my-app\pom.xml) has 1 error
[ERROR] 'build.plugins.plugin.artifactId' is missing. @ line 98, column 12

If you notice I used com.thirdshiftcoder.demoApp for my groupId on my external dependency jars. Yup, they are all in:
C:\Users\mtdew3q\.m2\repository\com\thirdshiftcoder\demoApp If I try to use the same name for artifact and dependencyId it puts 2 entries in the repository dir for some reason.

Please let me know if you have a cool tip. Is this sort of a good way to make a pom or would you do it a lot different? If I did it right, it probably is. Mr. MKYong seems to be a wizard!
Thanks Jeanne for helping! -jim

I will check back tomorrow after work some time. I have to reset. The real job is in 8 hours. I am at gmt -5 I think. good night.

E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 761
    
    9
Do not add jars to eclipse's build path when using maven. If you are using maven then all your dependencies should be in the pom and you should not need add any extra classpath entries either.
If you have jars in your repository (in the right folder structure) and maven still says it can't find them then check that your maven settings file is pointing to the correct folder for the repository containing the jars.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15


I wish when I posted xml it would indent. Maybe I will try code tag.


The JavaRanch message editor is rather like microsoft Word or HTML in that its normal mode of operation is to re-flow text for readability. The "Code" tag overrides that behavior for pre-formatted/structured text. Because in such cases "readability" means that spacing is retained "as-is".

You shouldn't attempt to control Maven via classpath. Also, I don't recommend coding things like the source and target Java versions as variables.

Maven's method of operation is designed so that if you did a "mvn clean" and zipped up the results and sent it to some other place on the other side of the Earth that whoever receives it should be able to unzip that project, do a maven build and create a module that is byte-for-byte identical with what you get when you do a build, regardless of what java files (and to a certain degree, what versions of Java and Maven) are installed on the destination computer. That is its greatest virtue, as it was specifically created to allow multiple people on far-flung continents to become immediately productive and to ensure a predictable project architecture.


Customer surveys are for companies who didn't pay proper attention to begin with.
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
Hi-

Thanks E. Armitage and Tim.

I am going to get a book on Maven. I am definitely interested in the technology. The way Tim described what Maven is used for is very cool.

I am going to not use the complicated part of the code in my pom by MKYong. I won't post another reply until I get my project working. I have my hibernate and apache derby demo up and running but without a way to package it, the project isn't as interesting.

Thanks for the cool tips.

Have a nice week,
jim
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

I have my hibernate and apache derby demo up and running but without a way to package it, the project isn't as interesting.


Maven can help there as well. For a WAR project, the command "mvn clean package" will clean out any residual junk, compile, run the war:war goal and result in a ready-to-deploy WAR in the target directory.

I go one step further. I have the RPM plugin installed, so the "package" goal not only creates a WAR, it bundles it up into an OS install package for the Redhat Package Manager. Aside from keeping inventory records in the OS package database, the RPM also allows more intelligent deployments, such as cases where I need to also create an external data directory for the webapp to hold uploaded files in.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: beginner maven question classpath and deployment
 
Similar Threads
The jar synchronization issue seems to be out of hand.
LOG4J messages in red about missing configuration file which is IN place
JUnit Test
org.springframework.beans.factory.BeanCreationException
Exception while running struts2 application.