Results 1 to 8 of 8
  1. #1
    Cleeve is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default Need advice wrt Java email & jar

    I'll break the golden rule by starting with an apology for posting in the wrong section, but I can't see which group is correct for java mail problems. For anyone who understands Java mail, I think my question will be simple. My program works perfectly when initiated from a DOS box(or Command line) with the instruction "Java myProg" and it issues an email when certain events occur. Everything works when compiled into a .jar with the exception of emails are never sent. It doesn't look as if my firewall is stopping the email going through (might be wrong), so is there anything extra I should do to allow java-email to be sent from a .jar?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Need advice wrt Java email & jar

    There should be no difference between executing a program in a jar and from class files in folder(s).
    A difference could be that some files are missing from the jar file or are not on the classpath for the Java program.

    Make sure there are no errors. Open a command prompt, change to the folder with the jar file and use the command:
    java -jar <THEJARFILE>.jar to execute.

  3. #3
    Cleeve is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need advice wrt Java email & jar

    I tried this and obtained the same results except the jar file issued an error message. I don't understand why it should run OK as a command line but not as a jar.
    All the java and class files (that I have written) are in the same folder and the program is run from that folder. The CLASSPATH includes C:\Progra~1\Java\javamail-1.4.4\mail.jar.

    The module Postit.java calls "Mailer" as a low priority thread to send a simple email.

    The error is:
    Exception in thread "Thread-6" java.lang.NoClassDefFoundError: javax/mail/MessagingException
    at Postit.run(Postit.java:18)
    at java.lang.Thread.run(unknown Source)
    Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
    at java.net.URLClassLoader$1.run(Unkown Source)
    at java.security.AccessController.doPrivilaged(Native Method)
    at java.net.URLClassLoader.findClass(UnknownSource)
    etc.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Need advice wrt Java email & jar

    java.lang.ClassNotFoundException: javax.mail.MessagingException
    Where is the class mentioned in the error message? It's probably in a jar file that you need on the classpath.
    How did you execute the program when it worked? What was the classpath set to?

  5. #5
    Cleeve is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need advice wrt Java email & jar

    I thought the exception would be in the C:\Progra~1\Java\javamail-1.4.4\mail.jar. I import javax.mail.* in the program and it doesn't object during compile. I've tried adding a few things to the classpath but no change in the result. I have to go out now, so no testing until later.
    CLASSPATH system=
    .;c:\jdk;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;c:\Progra~1\Jav a\javamail-1.4.4\mail.jar;c:\jdk\cctv
    CLASSPATH user=
    .;C:\PROGRA~1\JMF21~1.1E\lib\sound.jar;C:\PROGRA~1 \JMF21~1.1E\lib\jmf.jar;C:\PROGRA~1\JMF21~1.1E\lib ;c:\jdk\cctv;C\PROGRA~1\Java\javamail-1.4.4\mail.jar;c:\PROGRA~1\javamail-1.4.4\lib\mailapi.jar
    (I just added the mailapi.jar but it has no effect)

    Presumably, the command line version never needs this Exception message as it sends the emails without problem. Everything should be identical as I call them both from the dos box as follows:

    C:\jdk\cctv>java m
    and
    C:\jdk\cctv>java -jar m.java

    FYI the batchfile to create the .jar contains:
    cd \jdk\cctv
    jar cvfm m.jar manifest.txt *.class

    and the manifest contains:
    Main-Class: m

    No doubt the missing error message is a fault I need to find, but if .jar worked like the command line it would not usually be needed.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Need advice wrt Java email & jar

    The java command with the -jar option ignores the classpath value.
    There is an entry you can put in the manifest file: Class-path:
    that could refer to other jar files that are needed
    Or you can create a shortcut or batch file that uses the java -cp comand line with the jar files and the class name on the command line.

  7. #7
    Cleeve is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need advice wrt Java email & jar

    I didn't know that the -jar ignores the classpath value, but just including it in the manifest didn't help. However, your suggestion has led to me "one-jar.sourceforge.net", which has shown that there is a lot I didn't know about JARs and Classpaths (and still don't). I need to study it for longer, but I think your are right about the way JAR handles Classpath being the cause of this problem.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Need advice wrt Java email & jar

    In general with Java you should never use the CLASSPATH environment for defining the classpath.
    You should define a classpath at runtime (either with -cp for non-executable jars, or in the MANIFEST for executable ones).

Similar Threads

  1. Advice on which java books to buy
    By patriotsfan in forum New To Java
    Replies: 5
    Last Post: 06-11-2011, 09:48 PM
  2. Just some advice on my first program in java.
    By allen1952 in forum New To Java
    Replies: 4
    Last Post: 12-22-2010, 02:32 PM
  3. send email using apache commons email
    By jnamendi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 10-14-2008, 05:55 PM
  4. Java Email Server 1.6
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-21-2007, 08:00 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •