File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a Class with a Static Method

 
Steve Dyke
Ranch Hand
Posts: 1602
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Dyke
Ranch Hand
Posts: 1602
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The examples I have found appear to be what I am using.

java ClassNAme.Methodname()
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1602
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1602
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1602
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1602
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 1602
    1
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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;
    ^
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic