I used for building and creating a jar file this command as l know
$ mvn -Pzinc package
and that also
$ mvn -f m2-pom.xml package
and it built successfully !
but i can't find a jar file ?
In your project's target directory you'll able to see the generated jar file.
Related
Right now, am using mvn package exec:java -DskipTests, which will compile my project just fine, and start a server.
Instead of restarting the server, if a .java file changes, would like to only re-compiling that lone .java file.
Does anyone have a reliable javac command that can compile a single .java file in a Maven project, and put it in target/classes?
Here is the project structure:
src/
main/
java/
foo/
My.java
and then it needs to be compiled to this location:
target/
classes/
foo/
My.class
the following bash script will compile the file if it has no imports.
#!/usr/bin/env bash
mkdir -p "$PWD/target/classes/foo";
javac "$PWD/src/main/java/foo/My.java" -d "$PWD/target/classes" -cp "$PWD/src/main/java"
but if I include imports in the file, then I need to use a classpath which includes the maven dependencies.
So when I set the classpath using -cp, I need to do something like:
-cp "$PWD/src/main/java:$M2_HOME"
does anyone know what I should add to the classpath to cover the maven deps necessary for a maven project?
Update
The only way I think this would work, is if Maven could bundle all the dependencies used in the first real build into one jar file, in a deterministic location. Then if I wanted to compile a single java file, I would reference that jar in the classpath.
Otherwise, it appears that maven append 100s of paths to the CLASSPATH, and has to do some careful calculation to figure out which version of which jar to include the CLASSPATH. It would likely be impossible to be sure which jars were included in the original build. So the only way to know would be for Maven to spit out a new jar of all the library deps in one jar file.
In my project I have this code that tell class loader to load Driver.class like so:
Class.forName(org.gjt.mm.mysql.Driver);
In Eclipse it runs with no problems and I have created the Jar file of the project. But I don't know how to insert the
mysql-connector-java-5.1.7-bin.jar
into a Jar file of my project. The folder structure look like this:
MANIFEST file:
Manifest-Version: 1.0
Main-Class: server.MultiServer
I am assuming that you finally just want to run your code as
java -jar myjar.jar
There are two options.
Keep mysql-connector-java-5.1.7-bin.jar next to your jar in the same folder and add classpath: mysql-connector-java-5.1.7-bin.jar to the manifest.
Copy all the classes in mysql-connector-java-5.1.7-bin.jar to your jar. Do not copy the jar but the classes in the jar. This is called a fat jar or uber jar. You can automate the same using maven shade plugin.
When you invoke your application jar add the -cp or -classpath option and provide the path to the dependent libraries, here the mysql-connector-java-5.1.7-bin.jar.
for example refer the below example
java -jar -classpath C:\myproject\lib\mysql-connector-java-5.1.7-bin.jar myproject.jar
I have created a program that creates a keystore. When I run this code on IDE, it runs perfectly. However, when I run the jar file - I get no errors but cannot find the keystore created.
Some additional info - The jar file has been created in the cmd prompt using the following commands:
javac -XDignore.symbol.file Keygenerate.java
echo Main-Class: Keygenerate >manifest.txt
jar cvfm myjar.jar manifest.txt *.class
The reason i use -XDignore.symbol.file option is because I am using some deprecated methods in my java file. My file runs perfectly fine on Netbeans but does not clean and build. This is the reason I am using command line to compile and create the jar.
You may need to add the manifest file to the META-INF folder of myjar.jar
Can you show the project and jar folder structures?
I have created a JavaFX project and am able to run it with the command
java -classpath [very-long-list-of-class-paths] danIDE.Main
However, when I try to use javapackager to create a jar out of it and run with java -jar out.jar, the prompt says Error: Could not find or load main class danIDE.Main
The command I used to create the jar is
javapackager -createjar -v -classpath [very-long-list-of-class-paths] -srcdir src -outfile out -appclass danIDE.Main
I have googled for a long time on this problem and I still couldn't find the solution. Can someone point me to the right direction? Thanks a lot!
Edit:
Here is the project structure.
and here is the exploded jar.
New exploded jar as #Garry requested:
Since you're using IntelliJ IDEA I suggest you let IDEA create your JAR file for you.
First, open up the module settings window:
Then, add a new artifact:
Select JAR From modules with dependencies:
Select your main class in the window and decide if you want to repackage all classes from your dependency JARs in your JAR (extract to target JAR option) or if you want to distribute them alongside your JAR (copy to the output directory and link via manifest option):
If you want to build it whenever you build the project (probably a good idea), click the checkbox for that:
When you next Make the project, the JAR will show up under out/artifacts:
If you didn't click the checkbox for building the JAR when you build the project you can build the JAR from the Build Artifacts option in the Build menu.
Can you try using below command? Make sure to update 'classes' folder to the Base directory of the files to package.
As you said in question that you are able to run danIDE.Main so I assume all the required classes are available in dist folder.
So create a folder out in the project parallel to dist
javapackager -createjar -classpath [very-long-list-of-class-paths] -appclass danIDE.Main -srcdir dist -outdir out -outfile out.jar -v
Updated: as per the uploaded screenshot: point -srcdir to dist, now the generated jar out.jar will be placed in out/out.jar
I have a directory which contains *.java files generated by a C++ project build.
I need a command line to create a *.jar file which :
contains the generated *.java files. These java files initially exist in a directory that I would specify to the command line;
the *.java files must be placed in a package that I would specify to the command line;
must be a Maven artifact, of type .jar and so which has a version, a groupId, and artifactId that I would specify to the command line;
I need this command line to be executed in a post-build for my C++ project build.
Thank you
Assuming you have a correct pom.xml you can start maven with the following command:
mvn clean install -Dproject.build.sourceDirectory=<your directory>
Though maven can be irritating when you don't use the default of "src/main/java"
Regarding 2.:
Passing the java package as a command line parameter is problematic, as the .java-Files must declare the correct package and must be located in a matching directory.
You could use the filtering mechanism of the maven-resources-plugin to replace a wildcard in your source files with the actual package, but you would need an existing pom.xml with the configuration for the plugin.