• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Won't This Simple Program Run?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}
}
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, bartender; I can't seem to format my previous message in a graceful way. Any hints?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic