wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Calling main method outside of its class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling main method outside of its class" Watch "Calling main method outside of its class" New topic
Author

Calling main method outside of its class

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear all,
I want to call a main method of a class, from outside of its package & want to supply string array as argument.
Is this possible!
I want to call main method of this class outside this package.
package p1;
public class Karun
{
int a;
public static void main(String[] args)
{
System.out.println(" "+args[0]);
System.out.println("Karun class");
System.out.println("The argument supplied is "+args[0]);
Karun k=new Karun();
k.a=Integer.parseInt(args[0]);
k.printA();
}
public void printA()
{

System.out.println("Karun class a's variable is"+this.a);
}

public void printKarun()
{
System.out.println("In karun class's printKarun method");
}


}




I want to call the above main method from this class.
package p2;
import p1.*;
public class Testingclass
{
public static void main(String[] args)
{
System.out.println("In main method of Testingclass");
Karun k=new Karun();
String[] args1={"10"};
k.main(args1);
//k.printKarun();
}
}


I run this command in
C:\sai>javac -d . Karun.java
C:\sai>javac -d . Testingclass.java

Corresponding packages p1 & p2 are created.
Corresponding classes are in P1 & P2.


Now when I run the class file
C:\sai\p2>java Testingclass
Exception in thread "main" java.lang.NoClassDefFoundError: Testingclass (wrong n
ame: p2/Testingclass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
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)


My path & classpath settings are:

JAVA_HOME---> C:\jdk1.5.0
classpath--->%JAVA_HOME%\lib;C:\sai;
path --->%JAVA_HOME%\bin;c:\sai;



So now is it possible to call the main method(of karun class) outside of its package( in Testingclass.java)
Regards.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

I run this command in

C:\sai>javac -d . Karun.java
C:\sai>javac -d . Testingclass.java


It's generally a good idea to have you source layed out like in your packages. And compiled like so...

C:\sai>javac -d . p1/Karun.java
C:\sai>javac -d . p2/Testingclass.java

Of course, you need to move your source to the right directories.

C:\sai\p2>java Testingclass
Exception in thread "main" java.lang.NoClassDefFoundError: Testingclass (wrong n
ame: p2/Testingclass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)


It is also a good idea (actually necessary) to specify the correct packages for the class too. And run like so...

C:\sai>java p2.Testingclass

Henry
[ December 05, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Instead of this:

Do this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Try

C:\sai>java -cp . p2.Testingclass

In other words, your working directory should be "sai", not "sai/p2", and you should run the class using the fully-qualified name. This is a general rule for working with Java -- you must always invoke a program using its fully-qualified class name.


[Jess in Action][AskingGoodQuestions]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
Testingclass is in package p2, therefore to call it you need to change directory to C:\sai and call
java p2.Testingclass
or you can call it from any directory using
java -cp c:\sai p2.Testingclass

main is no different to any other static method other than being used by the JVM as an entry point. Like other static methods it is considered better to call it using the class name rather than an instance of the class
i.e.
Karun.main(args1);


Joanne
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
when I give this command
C:\sai> java p2.Testingclass
it's working fine

the output is

In main method of Testingclass
10
Karun class
The argument supplied is 10
Karun class a's variable is10

So it can be concluded that a class's main method can be called outside of its class.

But why previously it was not working. That's what I'm not getting?
Thanks & regards.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by ramya narayanan:

But why previously it was not working. That's what I'm not getting?
Thanks & regards.


The error message is pretty clear, I thought.

You tell Java, "run the class named Testingclass". Java looks on the classpath and finds a file named "Testingclass.class", and thinks "Hey, that's probably what he means." So Java loads that class file, finds out that in fact it's not a class named Testingclass, but rather, it's a class named "p2.Testingclass". Java thinks "well, that's not right", explains this to you in the error message:

java.lang.NoClassDefFoundError: Testingclass (wrong name: p2/Testingclass)

and quits.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

Also, it looks like this isn't your first time using Java. But is this the first time that you tried to run something in a package? I am surprised that you haven't encountered this issue earlier.

Henry
 
jQuery in Action, 2nd edition
 
subject: Calling main method outside of its class
 
Similar Threads
How to call a protected method in other packages non-subclass
subclass methods
Unsupported major.minor version 49.0
Marcus Green - Exam 2 - Question 9
Static method problem