DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2010
    Posts
    6

    How to create package class

    Dear firend,

    I have created the packge file under /home/in/package/Time1.java
    Code:
    Time1.java:
    --------------
    
    package in.package;
    
    public class Time1 {
    
    public static String name="Package Testing";
    
    public void assign_name(String val) {
    
    name=val;
    }
    
    public String get_name() {
    return name;
    }
    
    public static void main(String args[]) {
    
    Time1 obj=new Time1();
    obj.assign_name("Testing");
    System.out.println(obj.get_name());
    }
    
    }
    Then I have tried to access this package file from home directory.
    Code:
    package_test.java
    ---------------------
    
    import co.in.bksys.packaging.Time1;
    
    public class package_test {
    
    public static void main(String args[]) {
    
    //in.package.Time1 time_test=new in.package.Time1();
    Time1 time_test=new Time1();
    time_test.assign_name("Leslie Samuel");
    String my_name=time_test.get_name();
    System.out.println(my_name);
    
    }
    }
    I have compiled this file from the home direcory.

    $ javac -classpath in/package/ package_test.java

    It was displayed this below error:
    =========================

    package_test.java:1: package in.package does not exist
    import in.package.Time1;
    ^
    package_test.java:8: cannot access Time1
    bad class file: in/package/Time1.java
    file does not contain class Time1
    Please remove or make sure it appears in the correct subdirectory of the classpath.
    Time1 time_test=new Time1();
    ^
    2 errors

    but if i used this below line in the package_test.java file insted of using this Time1 time_test=new Time1(); .
    It will work perfectly.

    in.package.Time1 time_test=new in.package.Time1();

    I know that its expecting some format of class file like , in.package.Time1.class inside the in/package/ directory.
    It wasn't there so displayed this error.

    How to create this knid of class file to appropriate directory ?
    How to make my program execute without giving full package name like , in.package.Time1 time_test=new in.package.Time1(); ?

    Thank you.
    Last edited by Hack; 06-23-2010 at 07:39 AM. Reason: Added Code Tags

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    In package_test.java you need an import in.package.Timer;
    You don't show it and then you do show it?

    When compiling package_test, the class path should point to the folder where the in folder is. IE at the start of the package path.

  3. #3
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,649
    Does this help at all?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  4. #4
    Join Date
    Apr 2010
    Posts
    6
    Thanks You so much norm . Now its working.

Similar Threads

  1. How to Create Forms Class in C#?
    By Complete in forum .NET
    Replies: 2
    Last Post: 04-18-2010, 01:09 PM
  2. Help with Error in class
    By velkropie in forum Java
    Replies: 5
    Last Post: 03-24-2007, 11:09 AM
  3. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 06:53 AM
  4. Abstract class
    By Manika in forum Java
    Replies: 1
    Last Post: 09-06-2000, 01:13 AM
  5. Create Multiple Instances of Class at Runtime
    By Eric Jenkins in forum authorevents.appleman
    Replies: 1
    Last Post: 04-13-2000, 10:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links