• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Creating JARs & Classpath Issues

 
Ranch Hand
Posts: 179
13
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'd raised a separate question on this yesterday but thought I'd start a new thread as my questions encompass things other than just Maven. I need help with finding out to create a runnable Java application from Eclipse.

I've put together a small GUI app in Eclipse that incorporates some Spring code and several of my classes are distributed across different packages. The application launches and runs fine in Eclipse (where I created it as a Maven Project and have successfully placed all of its dependencies in the POM file as well as specifying the Main class) but when I run Maven clean install from Eclipse and generate a JAR file nothing happens when I double-click on it: no GUI appears.

I went to the src/main/java directory where my classes are stored and compiled the main class but it threw up a number of errors saying it couldn't find the Spring code and couldn't find the classes I'd written myself that I'd specified in different packages within the same directory. I'm guessing this is a classpath issue but I have a few questions

  • Firstly, how do I compile these classes so that I just need to run the main class from the command prompt?
  • Secondly, why does this run fine in Eclipse? I've added Maven dependencies but not placed any external JARs on the build path
  • Thirdly, why use Maven at all if things like Spring and Hibernate JARs need to be added to a machine's classpath before the app that's dependent on them can run?



  • Any help on this is appreciated.

    Also: no Manifest-MF file has been created when I run Maven clean install. This should be something that gets created automatically, no?
     
    Simon Ritchie
    Ranch Hand
    Posts: 179
    13
    Hibernate Eclipse IDE Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    RESOLVED: I was unaware of the concept of the "fat JAR" file. For any users here who might be interested I was able to run a Maven-installed JAR from the command line by making the following changes to my POM (taken from here)



    I then simply ran a clean install from Eclipse, navigated to the target directory and ran the ONE-JAR file

    java -jar <name of file>.JAR
     
    Bartender
    Posts: 20842
    125
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Java implementation specs don't allow for finding JARs within JARs, as is necessary for webapps and complex stand-alone applications like yours. What Maven is doing is adding a custom classloader to your application to make those inner JARs part of the application classpath. That's why you have to use the plugin instead of just using the simple JAR-builder setup.
     
    Simon Ritchie
    Ranch Hand
    Posts: 179
    13
    Hibernate Eclipse IDE Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks. Out of curiosity how does this work for large-scale Java projects with numerous developers? Let's say there's a web service that uses Java in its application layer and uses libraries for Hibernate, Spring, Junit, etc. In the case of a web service there tends to be no JAR file so how is the application classpath managed? Presumably the server administrators or developers regularly update the classpath with references to the external JAR libraries that they're using?
     
    Tim Holloway
    Bartender
    Posts: 20842
    125
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Web Services are provided as web applications. Or, in some cases, as parts of larger web applications. Web applications are packaged as WARs or EARS and web application servers automatically put all jars in a WAR's /WEB-INF/lib directory in the webapp's classpath.

    Webapp servers are a species of animal that can contain multiple independent apps - other such creatures include OSGi containers. To ensure that the apps within the container are truly independent, each app has its own unique classpath. So on the one hand, if you have 2 webapps using 2 different versions of Hibernate, there will be no confusion over which versions of what get used where. On the downside, that means that every app deployed within the container has to have its own copy of those library jars even when they're all the same version of the jar. Actually, in the case of a webapp server, there's a parent component to each webapp's classpath that's common to all webapps in the container, but app-specific JARs should not be stored in that part of the classpath for reasons too extensive to mention here.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!