• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling main method outside of its class

 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of this:

Do this:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic