I think my jar to work needs mail.jar plus the others class files what to do?

subject:javax.mail.Message
I used
jar cmf SMTPClient.jar SMTPClient.class SMTPClient$SendAction.class SMTPClient$UpdateListener.class mail.jar SMTPClient.java

but gives (how include another jar in my jar and keep package structure?)

C:\Users\User\Documents\TextBooksDataFiles\Java Network Programming, 3rd Edition\jnp3examples\examples\19>jar cmf SMTPClient.mf SMTPClient.jar SMTPClient.class SMTPClient
$SendAction.class SMTPClient$UpdateListener.class mail.jar SMTPClient.java

C:\Users\User\Documents\TextBooksDataFiles\Java Network Programming, 3rd Edition\jnp3examples\examples\19>java -jar SMTPClient.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Message
        at SMTPClient.<init>(SMTPClient.java:115)
        at SMTPClient.main(SMTPClient.java:296)
Caused by: java.lang.ClassNotFoundException: javax.mail.Message
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        ... 2 more

Dani AI

Generated

The error means the JVM can’t see the JavaMail classes at runtime. Putting the file mail.jar as an entry inside SMTPClient.jar does not automatically make its classes loadable. is right that packaging your own compiled classes into a jar (for example using a wildcard) gathers those .class files, but that does not include or expose another JAR’s classes to the JVM.

Two practical ways to fix it quickly:

  • Run the JVM with both jars on the classpath from the command line (this is the fastest test). Example launches:

    # Windows
    java -cp SMTPClient.jar;lib/mail.jar SMTPClient
    
    # macOS/Linux
    java -cp SMTPClient.jar:lib/mail.jar SMTPClient

    Note: using the “-jar” launcher uses only the JAR’s manifest for extra classpath entries and ignores an explicit -cp you pass on the command line.

  • Produce a single runnable (fat/uber) jar that actually contains the mail classes. Do this by merging the compiled classes from mail.jar into your jar (or use a build tool plugin like Maven Shade / Gradle Shadow, or a packager like One-JAR/Spring Boot). Be careful: merging can create duplicate resources and has licensing implications.

Quick troubleshooting checklist:

  • Confirm mail.jar actually contains javax/mail/Message (list its contents).
  • Confirm your manifest is inside META-INF/MANIFEST.MF and its classpath entries point to external jars (relative to the SMTPClient.jar file location).
  • If GUI double-clicking fails but the -cp run works, the manifest/classpath approach isn’t being satisfied — use the -cp approach or create an uber-jar.

If needed, give the exact commands you used to build the jar and the layout of your files and I’ll point to the precise step that’s missing.

you can use this code instead of adding one by one class file

jar cmf SMTPClient.jar *.class

*.class will collect all the class file which is been created by your java file.

i hope this code will help you.

first time running jars[gui] non-success, needed anything else from mail.jar?

in shortcut
target:"C:\Users\User\Documents\TextBooksDataFiles\Java Network Programming, 3rd Edition\jnp3examples\examples\19\SMTPClient.jar"
startin:"C:\Users\User\Documents\TextBooksDataFiles\Java Network Programming, 3rd Edition\jnp3examples\examples\19"
the jar is in the
C:\Users\User\Documents\TextBooksDataFiles\Java Network Programming, 3rd Edition\jnp3examples\examples\19\lib\mail.jar

only from running [CLI] the- java SMTPClient .class -email delivered

this is correct ... how many lines must follow: SMTPClient?

Manifest-Version: 1.0
Class-Path: lib/mail.jar
Created-By: 1.6.0_15 (Sun Microsystems Inc.)
Main-Class: SMTPClient

in this case mail.jar needed ONLY outside the SMTPClient.jar?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.