Forums Register Login

Why Won't This Simple Program Run?

+Pie Number of slices to send: Send
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");
}
}
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
Sorry, bartender; I can't seem to format my previous message in a graceful way. Any hints?
+Pie Number of slices to send: Send
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.
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 767 times.
Similar Threads
I Still Get the Same Compile Error
class file contains wrong class error , i dont understand it so fandmental
Why Won't ThisCompile?
Running Jars
basic Q.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 00:05:33.