Javapackager: Cannot find or load main class - java

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

Related

Compiling a single .java file in a Maven project

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.

Can't find dependent libraries in jar

My java project uses javax.mail.jar. So, in netbeans, I created a new folder Libraries, copied the jar file there and added this in project properties. I added this folder to git and this runs fine on netbeans. But when I build the project with ant on another machine and run the project jar with
java -jar myproject.jar
I get the error
Exception in thread "main" java.lang.NoClassDefFoundError:
javax/mail/MessagingException
I can see that myproject.jar contains the dependent library
$jar tf TakServer.jar
Libraries/
Libraries/javax.mail.jar
...
I'm not sure what is going wrong. Any idea how to get it to run?
Solution: As #Gimby pointed out, the jar files from Netbeans and ant build are different. Netbeans includes a lib/ folder containing all dependencies in dist/ folder and its MANIFEST.ML adds lib/ to classpath. The Libraries folder inside the main jar is not used at all.
The ClassPath in the manifest points to the filesystem outside the jar.
It it not possible to use jars inside jars
https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Reproduce Eclipse JAR export with javac on Linux

I need to be able to reproduce the following Eclipse process:
Export
Runnable JAR file with option "Extract required libraries into generated JAR"
This results in a JAR that includes all the referenced libraries and they have been compiled.
However, when I use this command on Linux:
javac -cp lib/lib1.jar:lib/lib2.jar -d newJAR src/Main.java
I get a JAR that includes just the class files of my own code.
I need to find a command that could produce the same output as with Eclipse, on the Linux command-line.
What you are asking for here is non-trivial. You want to package all dependencies into the JAR and also have the class path set so that you can run a class from the new JAR.
Two plugins I have used in the past to do this are maven-assembly-plugin and oneJar plugin.

How to attach libraries to JAR file?

It works fine when compiling project, but after exporting it to a runnable jar and launching, it can't find external files and throws an error. What should I do?
Add external libraries to the manifest.mf:
Class-Path: . MyApp_lib/extlib.jar MyApp_lib/extlib2.jar ...
You could attempt building a fat jar that includes all the jars. It contains a custom class loader to load the jars referenced externally by your project.
Try using http://fjep.sourceforge.net/ plugin to build a fat jar.
You can export a java project containing jars using the File -> Export -> Other -> One Jar Exporter.
The jar thus exported works fine.
You have to keep all required jars in the classpath to run your jar. Run your jar like :
java -cp extlib/* -jar yourjar.jar OR java -cp lib1.jar:lib2.jar:.. -jar yourjar.jar
Make sure that while building the jar, you include all the used libraries(include everything from class path). This issue will happen when you refer a external jar.
You can include a classpath variable in the jar's manifest file.
JAR file classpath

How to build a distributable jar with Ant for a java project having external jar dependencies

I have a Java project in Eclipse with class MainClass having main method in package :
com.nik.mypackage.
The project also references two external libraries, which I copied in the lib folder in Eclipse and then added to build path using ADD JAR function. The libraries being one.jar and two.jar
This library is in lib folder in eclipse and added to the build path.
I want to create a executable JAR of the application using ant script. So that user can access my application using command:
c:>java -jar MyProject-20111126.jar
I know about the Eclipse plugin which directly exports a java application as runnable JAR. But I want to learn ant and the build process so manually want to create the build.xm.
You have two options from your build.xml. You can either unjar the library jars and then bundle their contents with the code compiled for your application. Or, you can put the library jars on the filesystem and supply a ClassPath entry in the manifest file of the MyProject-2011126.jar file.
If you set the classpath in the manifest remember that the path you supply is relative to the MyProject-2011126.jar.
one alternative:
Instead of having only a jar, you build mutiple jars (your jar + libs) +batch file.
So, your built package can be like this structure:
-/package/bin/app.bat
/package/lib/my.jar
/package/lib/one.jar
/package/lib/two.jar
In app.bat you just have the same as your code
java -jar MyProject-20111126.jar
PS: if you want to start learning built tools, ANT may be a bit tool old. I suggest http://maven.apache.org/
Please try one-jar. It helps to redistribute everything packaged as single jar and comes with ant-task . See Easiest way to merge a release into one JAR file.

Categories

Resources