Link to home
Start Free TrialLog in
Avatar of coolgem
coolgem

asked on

How can I write a program in WebSphere that does not have a package name, yet can access the J2EE stuff, maybe even (if Websphere or the groundrules permit) use existing programs that are using som

Please advise as to what is the most practical way to go with reference to the issue below : Thank you in advance
Can a batch program(s)(two be run from Windows XP) be developed from
WebSphere 5.0.  I have to use a public static void main in the program. Is also
possible for me to use existing classes that are located in an ejb project of
WebSphere?  I have to access a couple of databases, plus some business rules, and  ultimately having to send emails to selected people.  Since JavaMail is from the javax.mail.*, that means it is J2EE.  All of these programs have the name of the package of where the programs reside in WebSphere.  Also, one program that connects to a databases thru use of JDBC  and uses  below:
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.sql.DataSource;

How can I write a program in WebSphere that does not have a package name, yet can access the J2EE stuff, maybe even (if  Websphere or the groundrules permit)   use existing programs that are using some EJB(s).

If  I can’t use the EJB stuff can I still use the javax mail and sql and jdbc that I will
have to develop in a program that  can be executed from a windows Schedular?
It seems  that  the main program cannot have a package statement. When I test and
from DOS, it will fail because of a NoClassDefFoundException.  Is there
something I’m supposed to do with a jar file in WebSphere to accomplish my
programming objective.  

Initially I want to access two databases, and send an email using JavaMail to myself.
This will prove that I can accomplish my mission before I get into the much larger complexities of writing another program(s).
 
 
   
ASKER CERTIFIED SOLUTION
Avatar of KeithWatson
KeithWatson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of coolgem
coolgem

ASKER

Thank you for your explanation?
The problem w/ EJBs that are running in WS is the have a  PACKAGE statement. The statement will cause a NoClassDefFoundException because the stand-alone program will  be run from DOS or a schedular?  Also are there any examples for JNDI?  What is it?  Do I deploy jar and ear files? If so how do I do sthat?  Any examples? Are you saying that I cannot write the program in WebSphere without using a package statement.  
Hi Coolgem, I'm not entirely sure what you mean by the "package" statement. Do you mean "package" as in the Java keyword, i.e. package com.sun.packagename; or something else? If it is the Java package statement, then I'm not aware of any particular stipulation regarding the package statement in Websphere, but it is good practice to place your classes in a package.

What I mean about a standalone program is one that would not involve an EAR file at all; however, were it to use an EJB, then it would use JNDI to get a remote reference to an EJB and then call it. The EJB itself would be deployed in Websphere using an EAR file. This, I guess is not your problem, since your concern is using EJBs that already exist in Websphere. JNDI stands for Java Naming and Directory Interface, and basically works as a layer of indirection between a common name, i.e. "MyEjb" and some network accessible component, which could be almost anything including an EJB. It abstracts the fact that an EJB lives on a physical computer, with a physical IP number. The idea is that if network administrators want to change the machine the EJB runs on they can do this without affecting any client programs, since they use JNDI to look up where the bean is hosted.

This page gives an example of looking up JNDI to find an EJB in Webpsphere.

http://publib.boulder.ibm.com/infocenter/wasinfo/index.jsp?topic=/com.ibm.wasee.doc/info/ee/ae/rnam_example_prop3.html

Avatar of coolgem

ASKER

Hi KeithWatson,
Thank you much  for your time. The package statment is a Java statment inserted into the java code as the first statment. Of course WebSphere does not provide the actual c:\...\...\... where the code is places by Websphere.  As a result if I try to run the program I get a NoClassDefFoundxception.  I believe it is because of the package statement.  In other words, can I get a program that access a a couple of databases,and base upon some business rules sends email to customers.  

The program has to be scheduled to run daily.  

The reason, i thought about using EJBs is because some other programmers were using them(I thought maybe I could save myself some coding, although I never used EJBs before). Actually i've only written about 5-6 programs using JSPs and Sevlets. My exp is limited.

   

Hi CoolGem,

You are able to create a standalone Java program with classes in packages. Also, it is likely that your class is not available on the classpath to WebSphere; this is more than likely why you are getting the NoClassDefFoundException. IBM have recently provided a piece of kit to help with this specific problem.

http://www.alphaworks.ibm.com/tech/jarclassfinder

In my opinion, here are your best options:

1. Create a standalone program that does all the work and doesn't use EJBs at all.
2. Create a standalone program that can be scheduled by some scheduling program (cron, Windows Scheduled Tasks) that invokes EJBs to do its work.

Either would work fine. The benefit you get of the second is the benefits the EJB container provides (transactionality across multiple resource managers, deadlock detection, connection pooling of database connections, security amongst others). If you don't need any of the above services, you won't save yourself coding; in fact, the amount of code your write will increase a little and you'll have to understand a number of additional concepts.
Avatar of coolgem

ASKER

If I create the standalone without EJBs, are saying that I need the jarclassfinder?  When I produce the code in WSAD 5.0, A PACKAGE STATEMENT is produced in the first line of the java program.  Because the  package statment does not provide full c:\...\... that is  why I thingk I get the noclassdefoundexception error.  

However when I take the package statement and try to compile it out of DOS I get the below errors from running javac testMail.java:
 as an ex. first error says javax.mail does not exist. for the import javax,mail.*
The imports were not accepted
there are 15 more errors.  

Thank you so much for your time.
Hi Coolgem,

The fact that the package statement does not specify a path is not the reason for the exception. A package statement declares a namespace only, not a physical location. Java locates classes using a classpath, which is in the case of a standalone program passed as part of the command line, for example:

java -classpath c:\classes.jar MainClass

WSAD removes the need to use the command line by providing IDE support for doing this, which I will describe below:

The likely problems in your standalone program are that you have not included the javamail jar and the activation jar file on your classpath.

Download the Java Activation Framework jar here http://java.sun.com/beans/glasgow/jaf.html
Download the Javamail jar here http://java.sun.com/products/javamail/downloads/index.html

Right click your project in WSAD and select Properties. Go to the "Java Build Path" option on the left of the dialog and select the "Libraries" tab on the right. Click "Add External Jar" and navigate to your downloaded jars. Adding these makes the classes in these JARs available to your program.
Avatar of coolgem

ASKER

Hi Keith,
How can I get you more points? You desrve a few thousand for knowledge and patioence.
Does a deployment descriptor do the same or similiar thing? When potentially using EJBs, that is under the j2ee umbrella? If so does a deploymnet descriptor somehow tie in because Java mail and EJBs are under the J2ee umbrella?  Is a deployment descriptor  a different or better mechanism (because of JNDI?) to attach classpath data for my standalone program that will use java maill and maybe use the EJBs that currently exist in the system(in WSAD 5.0)that i'm working on?

I looked up "jaf" and found it in a *.jar (I cannot make out the data, however i loated a "jaf". But i'm not sure what it means or what effect it has on the system.      
     
Thank you for you time
Avatar of coolgem

ASKER

You stated above "Right click your project in WSAD", an d select properties.  Does "project" mean WebSphere project or the name of my programinquestion.class folder?  
Anyhow I downloaded the jaf jar and javamail jar to c:unzipped\.  
Then I did what you said above:Right click your project in WSAD and select Properties. Go to the "Java Build Path" option on the left of the dialog and select the "Libraries" tab on the right. Click "Add External Jar" and navigate to your downloaded jars. Adding these makes the classes in these JARs available to your program.

I went into DOS where my *.classes reside(Websphere c:\......\......\.......\...........
I did java TEstMail and received a NoClassDefFoundError.







 
Hi Coolgem,

In your external build path window, it should then look something like:

mail.jar - c:\unzipped
activation.jar - c:\unzipped

I'm just checking to make sure you haven't unzipped the jar files.

Once these are in the path, you will be able to instantiate classes that reside in these jars. It may be that something else is now causing the NoClassDefFoundException. Can you post the line of code on which it fails?

Also, JavaMail does come under the J2EE umbrella; however, this does not mean you must run it in an EJB container. This is true of many of the other J2EE APIs. EJBs must be run in an EJB container, that's all. A deployment descriptor is a way for EJBs to describe themselves to an EJB container. A standalone program does not need one, and indeed cannot have one.

Here is a sample program. Remember that mail and activation jars must be on the classpath as described before:

package com.keith.mail;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Test {

      public static void main(String[] args) throws Exception {

          boolean debug = false;

          Properties props = new Properties();
          props.put("mail.smtp.host", "your smtp mail server");

          Session session = Session.getDefaultInstance(props, null);
          session.setDebug(debug);

          Message message = new MimeMessage(session);

          InternetAddress addressFrom = new InternetAddress("keith@email.com");
          message.setFrom(addressFrom);

          Address address = new InternetAddress("fred@email.com");
          message.addRecipient(Message.RecipientType.TO, address);

          message.setSubject("Subject");
          message.setText("Message Text");

          Transport.send(message);
          
      }
}




Avatar of coolgem

ASKER

I did what you said and downloaded the jaf and javamail jar files.
I still get a  NoClassDefFoundError.
Avatar of coolgem

ASKER

it looks like my jar files exist in c:\unzipped\jaf-1_0_2-upd[1]\jaf-1.0.2\activation.jar
and c:\unzipped\javamail-1_3_1-upd[1]\javamail-1.3.1\mail.jar
Coolgem,

I think I've missed something. I'm assuming you're running it from inside WSAD, which you can do. If you are running it from a DOS box or a shell, then you need to use -classpath to tell it where the classes are, so instead of:

java TestMail

you need

java -classpath c:\...etc...\mail.jar;c:\...etc...\activation.jar TestMail

Sorry about the confusion.

If this doesn't work, can you post your program so I can take a look at it?

ps: to run it as a standalone program within WSAD, select the class containing your main() method and then do Run->Run As->Java Application.
Avatar of coolgem

ASKER

Hi Keith,
I also did java -classpath c:\...etc...\mail.jar;c:\...etc...\activation.jar TestMail

but i still get a "exectuin in thread main NoClassdeffoundError

May I email it to you.  You will understand the reason when you look at the package statement at the top of the program.

I also did java -classpath c:\...etc...\mail.jar;c:\...etc...\activation.jar TestMail

but i still get a "exectuin in thread main NoClassdeffoundError
Avatar of coolgem

ASKER

Hi Keith,
I have everything working
I am able to run from DOS.
Thank you
Great stuff,

Glad to hear it.