• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

I cannot successfully execute my project jar file from the command prompt

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is where I am at:   My project (J3-09) runs great on netbeans10 (JDK11) and my windows7 machine.

However, This is what I see at the command line:

C:\dist2>java -jar J3-09.jar
Error: Unable to initialize main class REN.My_Frame
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/math3/
linear/RealVector

I have 2 jar library files in the dist2 folder:    commons-math3-3.6.1.jar, and jssc.jar

My research indicates that my main class is not able to find the jar libraries.    

What is the best way to solve this ?

Note that a solution also needs to be deployable to my Ubuntu 18.04 machine.

Thank you
Roy
 
Rancher
Posts: 3795
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the manifest file in the jar file have a Class-Path: entry?
 
Rancher
Posts: 179
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Maven, add the jar plugin:

(Add your own class instead of cli.Launcher)

And add the Maven Shade Plugin to create a full jar
(The name of this plugin is deceptive; it can shade, but the primary purpose is to build a jar that contains all your dependencies)


Add the compiler plugin:

And variables:

Finally, execute "maven package" and it will generate a jar in the target folder which will be runnable and contain all your dependencies. This is the easiest and most consistent way to create jar files.
 
Saloon Keeper
Posts: 22126
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're trying to put JARs within JARs, that does not work in standard Java. Only classes in the outer JAR get put into the application's CLASSPATH.

If you want to include other JARs within an executable JAR, then you need to add some additional code to extend the CLASSPATH. The easiest way to do that is to let Maven build the executable JAR using its executable JAR mojo Maven will add the extra logic. Just a regulat Maven JAR build won't do that, however, so you do need to build with the correct mojo.
 
Sheriff
Posts: 21954
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The shade plugin that Zachary mentioned bundles the contents of all dependencies inside your own JAR, making it a so-called fat JAR. This will work without any other JAR files, because their contents are placed alongside your own classes.
 
R Nordstrom
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I opened J3-09.jar with 7-zip.   This is the contents of MANIFEST.MF

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.10.4
Created-By: 11.0.2+9-LTS (Oracle Corporation)
X-COMMENT: Main-Class will be added automatically by build
Main-Class: REN.My_Frame


I do not see a Path-Class statement. Do I need one ?

Thank you
Roy
 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

R Nordstrom wrote:. . . This is the contents of MANIFEST.MF . . .

I presume it has an empty line at the end.

I do not see a Path-Class statement. Do I need one ?

Thank you
Roy

Do you mean classpath? You only need a classpath if you are dependent on some external resources.
 
Norm Radder
Rancher
Posts: 3795
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the other jar files are NOT inside of your jar file but are separate files that are in the same folder as your jar file, the Class-Path statement will allow the JVM to find and use them.
 
R Nordstrom
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a Class-Path line in a project jar from my NetBeans-8 projects.


This is what my manifest file contained:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.8.0_40-b26 (Oracle Corporation)
Class-Path: lib/jssc.jar lib/jna-4.1.0.jar lib/commons-math3-3.6.1.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: REN.My_Frame

So now I need to figure out why Netbeans-10 is not adding this line, or modify my current project jar/manifest file.  

I am not sure how to do either of these, but at least I know what is wrong.  

Roy
 
Norm Radder
Rancher
Posts: 3795
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the Class-Path statement is telling the JVM to look in the lib folder for the 3 jar files on the statement.  Your jar file would be in a folder that contained the lib folder.
 
R Nordstrom
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Every one,

I figured out the root problem.  Netbeans 9, 10 and early 11, have a bug.  When the clean/build is executed it does not add the appropriate Class-Path line to the manifest file.  Also it does not create the lib folder for your library jars.  This problem was solved in Netbeans 11.2.  By installing Netbeans 11.3 I now have a good command line launch in win7 and ubuntu 18.04.

Thank you
Roy
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting that, R! It's useful information about Netbeans so I added this post to the Netbeans forum.
 
Ranch Hand
Posts: 127
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zachary Griggs wrote://truncated to avoid full post quote


Although the topic is solved there're several issues to address:
1) As the error message was not "could not find main class" but a classdefnotfound of a dependency the issue was obviously not caused by a missing main-class manifest attribut - so, why even mentoined it? Did you read and understood the problem? Or did you may only jzst copied in some pre-written default text of maybe many?

2) Although building a fat jar often at least "make things work" it's far from an ideal solution. Aside from possible conflicts it can be compared to keep swallow painkillers instead of taking proper meds to cure: you're just tinkering with the symptoms instead of fixing the cause.

3)

This is the easiest and most consistent way to create jar files.


Uhm, no, it isn't. It's neither easy nor consistent and hence shouldn't be seen or used as such.
If you have an external lib within it's own jar don't mess with it but rather correctly reference it in the classpath.
Btw: This may work for a hobbyist dev - but most often licenses deny abusing their covered code that way on distribution. You just can't ship a big fat jar couple 100mb in size.
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic