• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error, when trying to call a method from different class.  RSS feed

 
Shakeel Jamadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi team,
I just created two classes, one with main method and one without main.
The main class is as follows:
-----------------------------------------------------------------------

-----------------------------------------------------------------------

The other class is:
-----------------------------------------------------------------------

-----------------------------------------------------------------------

I'm compiling these classes using the command prompt in windows 7.

There is no error when "MyMethod.java" is compiled but the error when I compile the "Test.java" is as follows:
-----------------------------------------------------------------------


E:\javabase>javac MyMethod.java

E:\javabase>javac Test.java
Test.java:5: error: cannot find symbol
MyMethod test = new MyMethod();
^
symbol: class MyMethod
location: class Test
Test.java:5: error: cannot find symbol
MyMethod test = new MyMethod();
^
symbol: class MyMethod
location: class Test
2 errors
-----------------------------------------------------------------------

Can anybody please help me out to understand the process of calling a method from different class.
Thank You.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go up in a directory structure and then compile with:
javac javabase/MyMethod.java
javac javabase/Test.java
 
mydukuri sreenu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Go up in a directory structure and then compile with:
javac javabase/MyMethod.java
javac javabase/Test.java



the two classes should be in same package after compilation also. then you will get result.

package com.xxxxx.rest;

public class Test {

public static void main(String[] args) {

Mytest mytest = new Mytest();
mytest.sum();
}

}

class Mytest {

public static void sum() {
System.out.println(10 * 10);
}
}


result is : 100

Note : the above code is in single java file Test.java

 
Dirk Lucas
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you normally should:




And the proces:



Hope this helps!
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dirk Lucas wrote: . . .
Hope this helps!
Afraid it probably doesn't help. The public access modifier may not enhance security at all. The other comments don't tell us anything we don't know already.
 
Dirk Lucas
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No public may not...but private or protected may...thats why I mentioned it;) Correct me if wrong but I am being teached that you can control what can or cannot be changed from the outside by using the right modifiers... Just another student trying to help.

And since he ask for and explanation on how the "calling" works in his last sentence I described what is happening in his code for him.

Can anybody please help me out to understand the process of calling a method from different class.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is the code won't compile in the first place. It may have something to do with which directories the files are in, but we don't have enough information to know that.
 
Dirk Lucas
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see...well I remade project in the same package and than it compiles. All I ll add to it;) *shuffles back into his corner"

run:
Sum = 20
BUILD SUCCESSFUL (total time: 0 seconds)
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it to compile because you got NetBeans to do it for you. We are here trying to persuade it to compile from the command line, which is easy enough if you know the directory structure.
campbell@campbellsComputer:~/java/javabase$ gedit MyMethod.java Test.java
campbell@campbellsComputer:~/java/javabase$ cd ..
campbell@campbellsComputer:~/java$ javac javabase/MyMethod.java
campbell@campbellsComputer:~/java$ javac javabase/Test.java
campbell@campbellsComputer:~/java$ java javabase.Test
Sum = 20
The two classes were straight copy‑and‑paste from the first post and there were no error messages from any JDK programs for me to hide. There were error messages from gedit, but that's a different story. Easy.
But we haven't got the information from OP to know why he couldn't get it to work. Maybe the directory structure was wrong. Maybe there was a tiny spelling error which has been missed. Don't know.
 
Shakeel Jamadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guyz!
Thanks for replying.
I've checked with directory organization yet the same error.
Both the java files are in the same directory.
--------------------------------------------------------------------------
And when I try to compile it this way, the error is:
-------------------------------------------------------
D:\Javabase>javac Javabase/New.java
javac: file not found: Javabase\New.java
Usage: javac <options> <source files>
use -help for a list of possible options
-------------------------------------------------------
What's going on?
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does New.java has this line as the 1st line in the file?

If yes, then command should be executed from D:\, not from D:\Javabase\

If no, then command should be executed from D:\Javabase, but like this

 
Shakeel Jamadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both the files contain

at first line. Same error.
CANNOT FIND SYMBOL.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Shakeel Welcome to the Ranch

You have to change your classpath
 
Shakeel Jamadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Rahul.
I've set the classpath in environment variables..
and the javac command is executable from any directory.
 
Shakeel Jamadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This program is simply working fine when I use the Eclipse IDE..
Giving me the perfect output.
What is the problem with command prompt?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us what you get from this command:-
echo %CLASSPATH%
Setting a system CLASSPATH usually does more harm than good. It instructs the system to look for all classes in a particular location. That is probably not where your javabase folder is.
By the way: Don't write Javabase; the original post said javabase and the two words are different. you should use javabase because the convention is not to use Capital Letters in package names.

Also tell us what happens if you try this command:-
javac -cp . javabase\Test.java
 
Shakeel Jamadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for Javabase.. Its (javabase)

I tried the commands you gave...
The results are:
-------------------------------------------------------

D:\javabase>echo %CLASSPATH%
%CLASSPATH%

D:\javabase>javac -cp . javabase\Test.java
javac: file not found: javabase\Test.java
Usage: javac <options> <source files>
use -help for a list of possible options
------------------------------------------------------
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are in D:\javabase. You should execute this command from D:\

If you execute this from D:\javabase javac looks for D:\javabase\javabase\Test.java which does not exist.
 
Shakeel Jamadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I execute it from d:
------------------------------------------------------

D:\javabase>cd..

D:\>javac Test.java
javac: file not found: Test.java
Usage: javac <options> <source files>
use -help for a list of possible options
:-(
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
javac -cp . javabase\Test.java
 
Shakeel Jamadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried something:
--------------------------

D:\>javac -cp . javabase\Test.java /*The file got compiled*/

D:\>java Test
Error: Could not find or load main class Test

D:\>cd javabase

D:\javabase>java Test
Error: Could not find or load main class Test
--------------------------
Got a "Test.class" file in 'javabase' directory
But cannot run it. Showing the above Error..
tried to run it from both directory d: and d:\javabase
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There ain't no such class as Test. It is called javabase.Test. Try
java javabase.Test

Also delete your system CLASSPATH.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can't execute that class inside the javabase folder. You need to be in its parent folder.
 
Shakeel Jamadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect... :-)
This command runs..

--------------------------
D:\>java javabase.Test
Sum = 20
--------------------------
What's with deleting system CLASSPATH... How to do it.. and what happens if i delete it?

And the parent folder in my case is I guess 'javabase' directory right?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by editing your system and user environment variables and telling us what you have in those two places for CLASSPATH please.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shakeel Jamadar wrote:Perfect... :-)
. . .
And the parent folder in my case is I guess 'javabase' directory right?
No. The parent folder, from the information you gave us, is D.

And … Success
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this Oracle link about the CLASSPATH.
Try deleting the CLASSPATH by writing the following instruction at a command line, and then delete your .class files and then compile your .java file again
set classpath=[enterKey]
See whether that works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!