| Author |
Creating a Class with a Static Method
|
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
I need to create a class that will hava a method to gather email addresses from a remote data source. I can accomplish this in WDSC for use in my web apps. However, I need this class so I can put it in a jar file that can be used from a remote call on our iSeries server. I have created a simple java class(just to get started and learn) and compiled it. However, when I try to call it from a command line I get NoClassDefFoundError. My class: My call and errors: C:\Demo>java -classpath 'c:\demo' Emailaddresses.mean() Exception in thread "main" java.lang.NoClassDefFoundError: Emailaddresses/mean() Caused by: java.lang.ClassNotFoundException: Emailaddresses.mean() at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
You may want to take a look at the Sun Java tutorial... http://java.sun.com/docs/books/tutorial/getStarted/index.html There are examples on how to use the "java" command. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
The examples I have found appear to be what I am using. java ClassNAme.Methodname()
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
Originally posted by Steve Dyke: The examples I have found appear to be what I am using. java ClassNAme.Methodname()
I would recomend that you don't ever use those websites again. It was probably written by someone who never ran a java program before. Henry
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Originally posted by Steve Dyke: The examples I have found appear to be what I am using. java ClassNAme.Methodname()
Do you have a link to these examples ?
|
Joanne
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Originally posted by Steve Dyke: The examples I have found appear to be what I am using. java ClassNAme.Methodname()
They don't usually; they usually use java ClassName The class you name is the one with a main method, and you can put command-line arguments after the class name. Those arguments reappear as "String[] args".
|
 |
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
That might be true. The Getting Started examples only give exapmples of Java Apps where a main method exists. And the call is simply: java ClassName. Can I have a static class without a main method and if so how do I call a prticular method in my class?
|
 |
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
Originally posted by Joanne Neal: Do you have a link to these examples ?
http://www.leepoint.net/notes-java/flow/methods/50static-methods.html
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
Can I have a static class without a main method and if so how do I call a prticular method in my class?
Yes. You can have a class without a main. And it can only be called from another class. BTW, I read the links. It was showing you have to call the method from another class -- not using the "java" application. Henry
|
 |
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
Okay I have added a main method and everything works okay as long as I comment out the line: package demo; If I have it in my code I get the NoClassDefFoundError again.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
The package name is part of your class name, so if you have package demo; at the top of your file your class name is actually demo.Emailaddresses, so your command line should be java demo.Emailaddresses
|
 |
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
Originally posted by Joanne Neal: The package name is part of your class name, so if you have package demo; at the top of your file your class name is actually demo.Emailaddresses, so your command line should be java demo.Emailaddresses
Still returns a NoClassDefFoundError for some reason. Any other suggestions? I really thank eveyone for the help.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Please explain exactly how you are compiling your class, and which folder you are invoking it from. You should always invoke demo.Foo from one folder "up" from demo, and you call it by saying java demo.Foo There are several ways to compile Foo; let us imagine that demo is an immediate subfolder of "java_apps". 1: Create a folder called demo inside java_apps. Put the Foo.java file inside demo. Navigate to demo, call "javac Foo.java".2: Create a folder called demo inside java_apps. Put Foo.java inside java_apps. Navigate to java_apps. Call "javac demo/Foo.java".3: No need to create the demo folder. Put Foo.java inside java_apps and navigate to java_apps. Compile with "javac -d . Foo.java". The -d bit will create any folders necessary and the . bit means look for files in current directory.I am sure there are other ways to compile classes. You may need to compile several simultaneously with "javac ... Foo.java Bar.java" or similar. [ September 11, 2008: Message edited by: Campbell Ritchie ]
|
 |
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
That worked. Thanks. My next step is to import the jt400.jar. I have it in my demo folder. Added the following to my class. import com.ibm.as400.access.AS400JDBCDataSource; At this point it compiles ok. However, when I add a line of code in the main method like: AS400JDBCDataSource datasource = new AS400JDBCDataSource("gvas400"); to access the methods of jt400.jar it fails to compile. Error: C:\WDSC\sdk\bin>javac c:\demo\Emailaddresses.java c:\demo\Emailaddresses.java:0: Class javax.sql.DataSource not found in class com .ibm.as400.access.AS400JDBCDataSource. package demo; ^
|
 |
 |
|
|
subject: Creating a Class with a Static Method
|
|
|