Question

I wanted to create and java desktop application which can display a .shp (Shape File) and edit the attribute table of that shape file. I was following this quick start tutorial on geotools web site to learn the part of displaying the shape file in a application. I successfully created the app in the tutorial and i could display a shape file in that.

Now i want to do the same in the java swing application i have already created with few other functions. I have a JFrame in which i want to display the map(shape file). I copied the code & all the required libraries in maven project to the java swing project but it gave me this error.

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/vividsolutions/jts/geom/Geometry

I think it's not possible to run the copied code without the pom.xml file in the maven project.

I might be wrong in calling it "maven application" or "maven project". I have very little knowledge in maven but a fairly good knowledge in Java SE.

I simply want to do is to display the map in the java swing application as i did in the maven application.

Can any one help me on this?

Thank You!

Was it helpful?

Solution

Maven is a program that handles project dependencies (it's lots of things, but this is its most relevant feature for you).

In your case, Maven knows that your project requires the gt-shapefile artifact (Maven's term for jars and jar-like things), because you told it so. But Maven will look up the pom.xml file for gt-shapefile and learn that it also needs gt-data, gt-referencing, and jdom. And then it will look at the POM for each of them and discover more dependencies until it's gathered the complete set.

Once Maven figures out all the libraries you need, it can perform a build (in this way, it's like Ant or your IDE, running the Java compiler). Once the code is compiled (or packaged, if you have Maven make a jar), Maven is done and has nothing to do with the completed product.

Swing is a standard graphics library. It's a compile-time dependency if you're writing code that uses it's classes, and when you're running that code later, Java will need to find an link with the Swing classes.

So for terminology: You can describe a project using Maven or Ant or NetBeans (which is a layer on Ant) or Eclipse. You can use any of these project-building tools to compile code that uses Swing classes. In fact, since the code Swing classes are part of the standard library, you don't need to mention any jars at all to work with Swing.


OK, now for your actual question.

One option for you is to just use Maven. Convert your existing application to a Maven project by copying its files into the Maven-style structure. There's a page on using Maven with NetBeans that may help. Basically, Java source code for the application goes under src/main/java (package-name directories start there), non-code things (like image files) go in src/main/resources, and stuff related to unit tests go in src/test/java and src/test/resources. If your application is simple, this shouldn't be too hard and you may find Maven convenient to use.

If your project is complex, or shared with people who don't use Maven, or you just don't want it, you should figure out what dependencies you need. I'm assuming for these instructions that you have Maven installed and are in a Unix shell.

Go to the root directory of the Maven-based Geotools project (the directory containing its pom.xml). You can copy all the dependencies out with the command

mvn dependency:copy-dependencies -DoutputDirectory=jars

This will create a subdirectory called "jars" containing every jar required to build the project (including transitive dependencies). Now simply link those jars to your existing application (or verify that they're already linked).

If you're curious, you can run

mvn dependency:tree

and Maven will print the dependency tree in text for you, so you can see why each jar is needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top