CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    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();
    	
    	}
    
    
    }

  2. #2
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    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 :-)

  3. #3
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    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
  •  





Click Here to Expand Forum to Full Width

Featured