-
December 21st, 2014, 10:20 AM
#1
import/package beginner's problems
I use terminal(newest JDK) in Ubuntu linux to compile source files. I Would like to compile these 2 files but when I try to compile them I get "package net.sssw.test does not exist..."
In which folders should I put the .java files and what commands must I type in terminal to compile and run this program?
Code:
package net.sssw.test;
public class Second{
Second(){
System.out.println("from constructor");
}
void f(){
System.out.println("from second!");
}
}
Code:
import net.sssw.test.*;
public class HelloDate{
public static void main(String args[]) {
System.out.println("from main!");
Second ob1 = new Second();
ob1.f();
}
}
-
December 30th, 2014, 09:16 AM
#2
Re: import/package beginner's problems
Hi,
It depends on what you are trying to do.
It seems you want a class Second in one location (./net/sssw/test) and the other HelloDate in another (./).
In such case it won't work, unless methods in Second class are set to public, ie:
Code:
public Second(){
System.out.println("from constructor");
}
public void f(){
System.out.println("from second!");
}
That's because if you do not set any access modifier, it takes, by default, package access. Such functions cannot be accessed from outside the package, which means they cannot be accessed from any class in other directory.
To compile and run in that case, HelloDate class could be in any location (e.g: ~/java/HelloDate.java), but Second class must be in ~/java/net/sssw/test/Second.java.
Then, compilation should be run from ~/java in this way:
[hostname]:~/java> javac HelloDate.java
To run, from same location
[hostname]:~/java> java HelloDate
Hope this helps,
Regards
Please, correct me. I'm just learning.... and sorry for my english :-)
-
December 30th, 2014, 09:43 AM
#3
Re: import/package beginner's problems
I solved this problem already, I thought this is a dead forum.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|