I have project where in my src/main/resources I have two mapping files.
Foo.hbm.xml, Bar.hbm.xml
When running my main class from eclipse, all works fine. When exporting to jar file using maven assembly plugin, I cannot create session factory due to missing mapping files. Error:
Exception in thread "main" org.hibernate.MappingException: Resource: Foo.hbm.xml not found
I am not using spring altough I also tried with Spring and I have same problem Spring's session factory cannot find mapping when running from jar. My implementation is following. Code:
Configuration hibernateConfig = new Configuration();
hibernateConfig.configure("hibernate.cfg.xml");
hibernateConfig.addResource("Foo.hbm.xml");
hibernateConfig.addResource("Bar.hbm.xml");
hibernateConfig.setProperty("hibernate.connection.url", conf.dbUrl);
hibernateConfig.setProperty("hibernate.connection.username", conf.dbUsername);
hibernateConfig.setProperty("hibernate.connection.password", conf.dbPassword);
I have also tried to add mappings trough hibernate.cfg.xml but it did not work same result, works in eclipse but not when using runnable JAR. My maven assemly config is following.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.app.EntryPoint</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
I create JAR with mvn assembly:assembly and when I open it mappings are there in root (/) of a JAR.
EDIT
Facepalm moment after looking at everything for 6 hours. Foo.hbm.xml was wrongly spelled so mapping was called MOTAccounts.hbm.xml and I used MotAccounts.hbm.xml.
What threw me off of this possibility was that everything worked in eclipse, but it didn't worked in a JAR. Like I said face palm moment.
Related
I'm building a self contained trading simulator using the quickfix/j library. Up untill now I'd been using mvn package, then the intelli J "Run button" to run my program from an entry point within my client-application class. I tried using the java -jar target/.....1.1.0.jar . and get the following error
java -jar Broker/target/Broker-1.0.0.jar
Error: Could not find or load main class Broker.ClientApplication
Caused by: java.lang.NoClassDefFoundError: quickfix/Application
I thought the error might have something to do with my pom file not fetching a dependecy properly. So to make sure I ran the ordermatch example from the quickfix/J github, but i get a similar error.
java -jar /homes/antonga/IdeaProjects/Desktop/quickfixj-parent/quickfixj-examples/ordermatch/target/quickfixj-examples-ordermatch-2.1.1-sources.jar
no main manifest attribute, in /homes/antonga/IdeaProjects/Desktop/quickfixj-parent/quickfixj-examples/ordermatch/target/quickfixj-examples-ordermatch-2.1.1-sources.jar
To be clear using the Intellli J "Run" button inside the main calss works percfectly even for the ordermacth example. From what I gather the command IntelliJ uses is something like this
"/path/to/java/" "-javagent/.../.jar" "/pathtolocalmavenrepo/quickfix-core.jar "/pathtolocalmavenrepo/anotherquickfixdependecy.jar" ....."more quickfix dependency jar files" packagestructure.Main
I don't see why this would work but my execution wouldn't. I can include my pom files and other info if this would help. I'm also using a multi-module maven project but that doesn't seem to the problem.
Turns out I was beeing a noob. The Maven package lifecycle bundles the specified class files without the dependencies into a jar. I needed to create an uber jar with all the necessary bianries, and then run that. See the SO question create excecutable jar with dependencies using maven
What is required is the following:
java -classpath <list-of-all-jars> <main-class>
Where <list-of-all-jars> is a ; (Windows) or : (*nix) separated list of all jars needed to run your program (quickfixj jars, your own jar and any jars needed), and <main-class> is the fully qualified class name of your main class (the class that has the main entry to your application)
You have to create an executable jar.
You can use Maven to do this. In your pom.xml can use maven-assembly-plugin to generate your executable jar
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>myour.main.Class</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
We are working on Automation Testing which uses selenium and Junit . Our requirement is to make an executable jar file using Maven. Project Structure is look like this
Project
|src\main\java
|src\main\res
|src\test\java
|testmain
|MainTest.java
|src\test\res
|target
|pom.xml
We are using maven assembly plugin to create jar file
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>testMain.MainTest</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>`
we can able to create jar file(SampleMaven-0.0.1-SNAPSHOT-jar-with-dependencies.jar) using this command .
clean package assembly:single
But when we try to run it using
java -jar SampleMaven-0.0.1-SNAPSHOT-jar-with-dependencies.jar
we are getting this error
Could not find or load main class testMain.MainTest
Please help me to resolve this error
EDIT:
As i mentioned above ,i couldn't have MainTest.java in src/main folder since our project is only about automation testing which uses selenium primarily . selenium/junit is test framework which does not work under src/main . Please let me know if there is any other approach to resolve this conflict.
Place all the files that are supposed to be part of the packaged jar into /src/main, not /src/test.
/src/test should contain your project's test files (unit tests etc) and are not part of the final artifact. That's why java cannot find the class in the jar.
I'm using Maven to build a project I'm working on. The build works fine, the problem happens when I run the generated JAR. The error says the main class com.apress.springrecipes.sequence.Main cannot be found. But looking at the folder structure I'm not seeing why the Main class can't be found.
Here's the folder structure:
myJar.jar
--com
--apress
--springrecipes
--sequence
--Main.class
--META-INF
--MAINIFEST.MF
The MANIFEST.MF file has the main class attribute set to com.apress.springrecipes.sequence.Main. But it looks like the JAR can't find it even though the class file is located in the JAR.
Here's the relevant portion of my pom.xml that handles the build:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>C:/Users/Graham/.m2/repository/</classpathPrefix>
<classpathLayoutType>repository</classpathLayoutType>
<mainClass>com.apress.springrecipes.sequence.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Why isn't the Main class being found in the jar even though it's there?
I'm not a Java dev, but I give it a try: what the compiler says and what you're checking is not the same thing. The compiler says it cannot find the "Main class", it does not say it cannot find the "Main.class file".
The file may be there, but is there a "class Main..." defined in it (with a public void main() method)?
Even I was facing similar issue, I took below actions and it worked.
Make sure your IDE and the JAVA_HOME environment variable are pointing to the same version of JAVA.
If you have any configuration files (.ini, xml etc) place those along with the JAR file in the same folder.
There is a java webapp with an embedded tomcat 7 that is built with this instructions (Using tomcat7-maven-plugin).
this webapp is launching with a jar file like this: java -jar webapp.jar
Question: How to run a main class after launching this embedded tomcat?
What you need is to setup your application's entry point. For this you need to configure your main class inside the jar's Manifest file.
Something like
Manifest-Version: 1.0.1
Created-By: <jdk_version>
Main-Class: fully.qalified.class.name.with.main.method
For more details on Manifest, take a look into this link here
For making this step part of your maven build cycle, you need to make some changes in the mave.jar.plugin. Something like
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>fully.qalified.class.name.with.main.method</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
The final jar that is created will have your main method as the applications entry point
If I understood your question correctly. In Eclipse, Right click the project and pick "Run on Server."
I have a rest endpoint used to return information about application (so far only app version)
But so far this info is hardcoded, and it's pretty easy to forget to change it.
I will be better to retrieve app version from pom or manifest file. Is there any project that brings such functionality?
Spring Boot can refer to the project version defined in pom.xml and expose it via REST using Actuator:
# application.properties
endpoints.info.enabled=true
info.app.version=#project.version#
Then accessing the /info URL (e.g. http://localhost:8080/info) will return:
{"app": {"version": "<major.minor.incremental>"}}
See also: spring boot/spring web app embedded version number
You better use build-in manifest.
new Manifest(Application.class.getResourceAsStream("/META-INF/manifest.mf"))
For the concrete impl-version:
new Manifest(Application.class.getResourceAsStream("/META-INF/manifest.mf"))
.getMainAttributes()
.get(Attributes.Name.IMPLEMENTATION_VERSION)
Using maven do not forget to create the manifest using:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
There is amazing project named Appinfo, please use it and enjoy! (It has an ugly page, I know - but it works :)
AppInfo allows to automatically feed your application with a current version number, build date or build number.
Also excellent Spring Boot Actuator provides feature named Info Endpoint which can publish version information to web or REST.
By default the Actuator adds an /info endpoint to the main server. It contains the commit and timestamp information from git.properties (if that file exists) and also any properties it finds in the environment with prefix "info".
You could use the resource filtering of maven or something like the maven-substitute-plugin.