aspose file tools*
The moose likes Beginning Java and the fly likes Why Won't This Simple Program Run? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Won Watch "Why Won New topic
Author

Why Won't This Simple Program Run?

Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57
I have 2 files. I call the method MyFunc in MyClass.java from main in MyTest.java. That's all I'm trying to do. I put them both in the same package, and it still won't run. I now get a run-time error saying "NoSuchMethod" when I run MyTest. Why??
Also, is there no way to call a method (via the import statement) without extending it's class, and putting both the caller class and the callee class in the same package?
Anyway, here's my 2 files:
Here's Mytest.java:
package com.util;
public class MyTest extends MyClass
{
public void main (String[] args)
{
MyTest obj = new MyTest();
obj.MyFunc();
}
}
And here's my MyClass.java file:
package com.util;
public class MyClass
{
public void MyFunc()
{
Suystem.out.println("Printed from MyFunc");
}
}
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

In MyTest.java, signature of the main() method should be:
public static void main(...

The only way to make MyClass.MyFunc() visible from a different package would be to either make it public or make it protected call it from a class that extends MyClass. The import statement will not help you do this in any way--that is not its purpose.
the following should work:

Compile with the following commands:
javac -d c:\java\com\util MyClass.java
javac -d c:\java\com\util MyTest.java
Run with:
java com.util.MyTest

Junilu - [How to Ask Questions] [How to Answer Questions]
Roy Tock
Ranch Hand

Joined: Jul 16, 2001
Posts: 83
Gary, the following also works:

From the command line:
<pre>
F:\temp>dir
Volume in drive F is BLAH
Volume Serial Number is 1111-1111
Directory of F:\temp

. <DIR> 07-24-01 7:05p
.. <DIR> 07-24-01 7:05p
MYTEST~1 JAV 198 07-24-01 8:52p MyTest.java
COM <DIR> 07-24-01 7:59p com
1 file(s) 198 bytes
3 dir(s) 1,410,453,504 bytes free

F:\temp>dir com\util

Volume in drive F is BLAH
Volume Serial Number is 1111-1111
Directory of F:\temp\com\util

. <DIR> 07-24-01 7:59p
.. <DIR> 07-24-01 7:59p
MYCLAS~1 JAV 169 07-24-01 8:53p MyClass.java
1 file(s) 169 bytes
2 dir(s) 1,410,453,504 bytes free

F:\temp>javac MyTest.java
F:\temp>java MyTest
Printed from MyFunc

F:\temp>

[This message has been edited by Roy Tock (edited July 24, 2001).]
Sorry, Roy. Doesn't seem like I did much better than you did.

[This message has been edited by Marilyn deQueiroz (edited July 24, 2001).]
Roy Tock
Ranch Hand

Joined: Jul 16, 2001
Posts: 83
Sorry, bartender; I can't seem to format my previous message in a graceful way. Any hints?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
It would be better if you continue the same thread, Gary, rather than starting several. This way several people don't give the same answer in various places. I'm closing this one.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Won't This Simple Program Run?
 
Similar Threads
Running Jars
I Still Get the Same Compile Error
Why Won't ThisCompile?
basic Q.
class file contains wrong class error , i dont understand it so fandmental