• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fail to import method from other class  RSS feed

 
Kew Jie Long
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i Create a folder name "virus". Inside that folder, there are 2 file which are test1.java and test2.java

Here is the code for test1.java:


Here is the code for test2.java:


There is no problem to compile test2.java, but it give me this error message when i tried to compile test1.java
/Users/kewjielong/Java/virus/test1.java:8: error: cannot find symbol
helloworld();
^
symbol: method helloworld()
location: class test1
1 error
[Finished in 0.9s with exit code 1]
[shell_cmd: javac "/Users/kewjielong/Java/virus/test1.java"]
[dir: /Users/kewjielong/Java/virus]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

Can anyone tell me where i did wrongly ? I follow every instruction on how to use class and method, but still can't figure out where is the problem :,(. Please help me.
 
salvin francis
Bartender
Posts: 1612
36
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would test1's main method (also note that this is static) have access to test2's method ?
 
salvin francis
Bartender
Posts: 1612
36
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit off topic: You should declare your class name with camel casing.

Think of class as a unit of code which does a clear task, the outer world does not have access to its inside methods as well as variables. But if it wants it can allow such access via access modifiers and inheritance.



PS : This is a very poor example my creativity is at a low level today
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As already noted your class and method names aren't following convention. It should be Test1, Test2 and helloWorld.

The main method cannot call Test2.helloWorld because the compiler doesn't know where that is. You need to unambiguously tell it which method you want to call. There are two ways to do this, as follows.

1) You prepend the method name with the class name, thus telling the compiler where it can find it.



2) You use a static import.



Both of these techniques only work with static methods, not instance methods.
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:. . .
2) You use a static import.


. . .
Surely that should read
import static virus.Test2.helloWorld:
In that instance I would prefer simply to write
Test2.helloWorld();

as you gave for your first solution.
 
Kew Jie Long
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the problem is something to do with the tools i used. i tried this in an IDE, it works. but it just can't be work when i tried to compile it on sublime text and try to run it on terminal. btw, i am using mac OS X.

The error message is :
/Users/kewjielong/NetBeansProjects/test2/src/test2/Test2.java:20: error: cannot find symbol
classTest1 hello = new classTest1();
^
symbol: class classTest1
location: class Test2
/Users/kewjielong/NetBeansProjects/test2/src/test2/Test2.java:20: error: cannot find symbol
classTest1 hello = new classTest1();
^
symbol: class classTest1
location: class Test2
2 errors
[Finished in 1.0s with exit code 1]
[shell_cmd: javac "/Users/kewjielong/NetBeansProjects/test2/src/test2/Test2.java"]
[dir: /Users/kewjielong/NetBeansProjects/test2/src/test2]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

Even though this file is work perfectly fine in IDE, it give error message on a text editor and give error message :
Error: Could not find or load main class test2 in terminal when i tried to compile.

So what is the problem ???!!
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us the instructions you are using to compile and execute that code.
 
Kew Jie Long
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, i created a file name Test2.java in netbean.

Here is the code:


And created another class in netbeans. Here is the code:



i can run it without any error. but i get error message which i stated in the last post when i tried to open the Test2.java file in sublime and try to compile and run it.
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't mix NetBeans and text editors.
That code should work similarly to the original code, but you would have the files in a folder called tet2 rather than virus.
Please show s the folder structure and which instructions you are using to compile the code.

This shows the sort of confusion which can arise when you try using IDEs too early in your career.
 
Kew Jie Long
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second code i show you is different from the file "virus". The second one is created by using IDE(netbeans), and run fine in IDE but output error message when tried to run it in text editor. i use command + B in sublime to compile the file, it output error message. And type "java test2" to run the test2.java file, because it had been compiled in IDE. but still, it give me an error message.

 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us assume you are using test2 as the package.
  • 1:Put the two files with test2 as package name inside the tet2 folder.
  • 2: Navigate to the test2 folder; if you give the ls command (on Windows run "dir") you will see various things including classTest1.java and Test2.java
  • 3: Give the following instruction: javac *.java
  • 4: Navigate to the parent folder with cd ..
  • 5: Run the following command java test1.Test2
  • If you get any error messages tell us what they are.
     
    Kew Jie Long
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    These are the message i got:

    Kews-MacBook-Pro:test2 kewjielong$ cd src/test2
    Kews-MacBook-Pro:test2 kewjielong$ ls
    Test2.class classTest1.class
    Test2.java classTest1.java
    Kews-MacBook-Pro:test2 kewjielong$ javac *java
    Kews-MacBook-Pro:test2 kewjielong$ cd\\
    -bash: cd\: command not found
    Kews-MacBook-Pro:test2 kewjielong$ cd\
    >
    Kews-MacBook-Pro:~ kewjielong$ cd netbeansprojects
    Kews-MacBook-Pro:netbeansprojects kewjielong$ cd test2
    Kews-MacBook-Pro:test2 kewjielong$ ;s
    -bash: syntax error near unexpected token `;'
    Kews-MacBook-Pro:test2 kewjielong$ ls
    build build.xml manifest.mf nbproject src
    Kews-MacBook-Pro:test2 kewjielong$ java test1.Test2
    Error: Could not find or load main class test1.Test2
    Kews-MacBook-Pro:test2 kewjielong$
     
    Campbell Ritchie
    Marshal
    Posts: 55786
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is cd .. not cd\\
    Please try again from after cd ..
     
    Campbell Ritchie
    Marshal
    Posts: 55786
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think I was mistaken saying java test1.Test2
    It should have been java test2.Test2
    Sorry.
     
    Mike. J. Thompson
    Bartender
    Posts: 689
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Mike. J. Thompson wrote:. . .
    2) You use a static import.


    . . .
    Surely that should read
    import static virus.Test2.helloWorld:


    Oh dear. That's what happens when I post too early in the morning.

    Campbell Ritchie wrote:
    In that instance I would prefer simply to write
    Test2.helloWorld();

    as you gave for your first solution.


    Yes, I was just including static imports for completeness. There are libraries designed to be used with static imports (such as JUnit and Hamcrest) so I think its important to know about it.
     
    Kew Jie Long
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i think the problem is the command i used in Terminal to run the code. I am using mac os X, i can't use cd.. but cd\ only. btw, i don't know which command i should use to run the code, since "java test2" is not working correctly
     
    Campbell Ritchie
    Marshal
    Posts: 55786
    164
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am surprised that you can't write cd ..
    It is cd .. NOT cd..
    I do not know where you get the \ from. Have never seen that command.
    I have already told you which commands to use only I made a mistake getting the name of the package wrong.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!