Standalone java class talking to a web application - java

i'm working on a quite complex java web application based on Spring framework and hibernate,
i have to create a normal java class (with main method) and export it in a .jar file; this class contains a procedure that will be manually schedulated monthly by an operator.
the results of the scheduling will be consulted on the web app pages (JSP).
i need to use the jar file like this : java -jar myfile.jar arguments ....
is there a way to do this ?
how to export all the dependencies derived from spring and hibernate ?
P.S. i tried export the jar file with MyEclipse but when i try to run the jar, it can't find spring classes

First of all the title is wrong, you need a standalone application.
Secondly, Every decent java IDE like should provide the option of exporting a jar. For example, in eclipse:
Right click the project
Choose export
Choose Runnable jar file
Another option is to use build tools like ant or maven.

Packaging dependencies is a topic which has no strict connection with Hibernate, Spring, Guice, or a Java library for generating random names for dogs.
The first thing you need is a build system, which I hope you have if
you are dealing with a quite complex java web application, and you
are not letting your ide build.
Once you have a build system such as Maven working correctly, you can
look to the packaging extensions. In Maven, this is called assembly
plugin.

Related

How to deploy a dymanic web project of java in eclipse to jenkins with less changes in code

I am new to jenkins so i don't know in depth about it. But in recent days we wanted to change from svn repository to GIT in which we have a java project created in dynamic web project. Now we have to deploy this project in jenkins. But i read that in jenkins only projects created in maven are accepted but not any other. Our project is a huge project and its created using dynamic web project. So if we want to convert to maven project will there be any code changes like many??
What are the jar files or any configuration files needed for the change??
I don't know whether this question is quite suitable to ask or not but we have less time and i am completely new to jenkins.Any suggestions or answers will be valuable to me
You will need to think about how you will run your application in production. Will you run it as a packaged war file in tomcat, jetty or another JEE server or will you run it using an embedded server (embedded tomcat etc) where the server is packaged with your application as an executable jar file.
Once you work that out you should think about how you are going to build the application, Maven, Gradle and Ant + Ivy are your three main options. Jenkins supports all of these options, not just maven. Besides being able to help you build your application in a standardized way, these tools will be able to help you manage your dependencies.
There really is not much configuration required once you know how to use the tools and your dependencies can be removed from your /lib folder (or wherever you are currently storing them) as a result.
So take some time to read up about each one, make your choice and then apply it to your project. It is a worthwhile investment and something you will use over an over again.
Jenkins makes a LOT of things easier if you have a Maven project, but you can specify a free form project where you can specify your own build command. This is not coupled with the git/svn repository.
Experiment with Jenkins. It is rather nice, and can do a lot of things when you learn it better.

What is the advantage to build an executable JAR

Today at work I came across something interesting. Say i have an old java project that were compiled with an ant build file and we have converted this project into a maven project. So now to build this project, we only need to do a mvn install.
When i do call
mvn install
I get a myproject.jar under the target folder, along with all the dependencies under a lib folder inside the target folder.
To run the executable of this jar I need to do something like :
java -classpath $classpath com.myproject.Mainclass $myArgs
Where $classpath is the path to all of my external libs and where $myArgs is the arguments that is passed to the main function.
I came across this website and I'm really considering to use the spring boot maven plugin to package my executable jar.
Wouldn't it be easier to execute it if all the dependencies are packaged in a single jar file ?
Why would I use the manual configuration vs the Spring Boot Maven Plugin for the executable jar ?
What are the pro and the cons of doing this ?
As the article you had linked covers with pros and cons how to do such single jar file packaging, I'll write out things that you need to consider if you want to use this approach.
Pros:
1. Simplicity of deployment
Users don't have to maintain any dependencies. All they need to do to run the app is get the jar file and execute java -jar file.jar.
2. No easy way to update dependencies by user
If your app uses some external dependencies, you can be sure they are in version that you have chosen. Using "classic" approach user can easily update it by himself to the version that may require some migration steps in your app.
Cons:
1. Size of final package
If your app has large dependencies, every update will require users to download the whole package,
even if dependencies haven't changed.
2. No easy way to update dependencies by user
To change a version of any dependency you will need to update the whole package, where using the old way you could update only the dependency jar.
Summarizing, if your app doesn't have any heavy (in sense of file size) dependencies, I'll personally use single jar file approach. Even if your dependencies changes frequently. It's a lot easier to change a single file, no matter if your app has to be updated or some of its dependency.

Netbeans generating .jar file

When I clean and build, Netbeans generates a .jar file for my Java project, and it uses a "lib" folder which has all of my external Jar files that I use. Can I somehow tell Netbeans to package these Jar files into the .jar it makes? So that I can run my project as a stand-alone .jar file?
I am using Netbeans 7.1.1 on Mac OSX
The short answer is no, the long answer is complicated.
Firstly, Java does not support embedded Jars (ie, you cann't simply add the depended Jars into the main Jar).
Secondly, uncompressing all the Jars and merging them into one will overwrite any resources/classes that share the same path/name. This may not be an issue in small projects but is a major issue in large ones (we have this problem with the project I'm working on at work).
One solution is to use something like One-Jar, which basically uses it's own boot class loader to all you to reference Jars inbedded within a single Jar.
I've had issues with this when using our own custom classloader, but otherwise it seems to work well.
It includes Ant support, so you can include it in your projects Ant build script, if you not using Maven, otherwise, you'll need to devise your own build process
There is no options in netbeans to do that.
There are some other options that a quick search would help, but requires manual intervention.

How to create an executable version of my OSGi project?

Is it possible to save my OSGi project (which comprises of few bundles) to a single executable file, something like an .exe file , so I can copy and run it to any PC that has a JVM.
I know that the normal method is to open a command prompt and install the required bundles/ jar files one by one. But since my project contains quite a few bundles that method seems tedious.
Thanx in Advance.
You can definitely do better than installing bundles one by one, even if you don't get as far as a single 'natively executable' archive like a .exe. There are lots of commercial and open source OSGi applications which ship as zip archives; the user unzips the archive and then either calls java -jar some.osgi.jar or runs a shell script.
There are a few ways the OSGi runtime can work out what bundles to install. Some are specific to an OSGi framework (such as Equinox or Felix) and others are more generic. If you're using Equinox, you can create a config.ini file and put it in a folder called configuration as the same level as your OSGi jar. List any bundles you want to start in the osgi.bundles property. The config.ini file can list all the bundles to start, and also any other configuration properties you might need.
Eclipse also allows you to define a minimal set of bundles in config.ini and use the configurator to start everything in the plugins folder. Similarly, if you're using Felix, any bundles in the auto-deploy directory will be automatically started. You could also look at Felix File Install, which allows you to drop bundles into a monitored folder to install them (once FileInstall itself is installed). Despite the name, FileInstall works on both Equinox and Felix.
This is similar to creating a complete OSGi application with Felix & Maven - the Sling Launchpad Plugin can be used to create an executable jar or war file that contains a specific set of bundles. If you're not using Maven yet it's not as simple as "saving your project to an executable file" but you could probably adapt the jar file generation mechanisms to your environment.
Eclipse has the notion of products. You could create a product from your existing bundles, and then your product can be exported as an executable. For details about the product see the Eclipse RCP tutorial from http://www.vogella.com/articles/EclipseRCP/article.html
The tutorial introduces the Eclipse RCP applications, but you do not need everything from there - it is possible to create a product without creating an entire Eclipse application.
This limits your choices as you are more or less bound to Eclipse Equinox as your OSGi runtime, but for creating an executable product from it might be a workable tradeoff.
Thanx.
Following the answers above I came across Chapter 9 in this book.
http://my.safaribooksonline.com/9780321561510/ch09#ch09
It had a walk through tutorial in exporting the project through the product configuration.
Its exactly what I needed. :)
If you use bnd(tools) then you can export a project to an executable jar either from the bndtools GUI or via the bnd commandline (bnd package xyz.bndrun; java -jar xyz.jar). The resulting jar file contains all the bundles, the launcher, and the framework.

How to share code between a Java Web app and a normal Java app

I have smallish personal project consisting of the following Eclipse workspace.
+
+-MyApp // this is just a vanilla Java Application
+-MyWebApp // this Dynamic Java Web Application (Tomcat)
+-MyCommonStuff // these are common classes
// Ex. Database access code & business classes
This is all well and good when I'm running on eclipse coz I can use Eclipse build properties to make the two apps reference the common project.
I am now thinking of how to deploy my app to a linux server and I'm wondering how to do it.
Can eclipse be used to build appropriate targets which can then reference the common stuff when running in a live environment?
Or do I have to learn how to use builders like Ant or Maven.
Thanks
You just need to add the common projects to the build path of the webapp project and to add them as web library dependency.
In the properties of your webapp project, go to Java Build Path > Projects and select the the common projects from your workspace to add them to the build path. Then, in the Java EE Module Dependencies select the projects which needs to be exposed in the WEB-INF/lib. That should be it.
You don't have to learn to use maven, but there are compelling reasons for doing so.
One of which is, maven can set up your Eclipse's build path up for you (And the next guy who has to work on your code, and the next guy...)
One simple 'mvn eclipse:eclipse' and all your build path setup is done!

Categories