Results 1 to 5 of 5

Thread: Accessing source code

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    102

    Accessing source code

    THis is probably a silly question but.....

    How can I include another class file in my program. I assummed that if there were several source code files in the same folder they could all be accessed from the source code file that had "main" in it. What I am probably looking for is the equivilent of 'include"myfiles";' in C++.

    Is there such a thing?

    I am using JBuilder University version. I have tried using Add To Project from the project menu but with no joy.

    Any help welcome

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I don't think there is such a thing in Java. You need to write your class up and compile it in order to use it in another class. And everything must be bound to a object, or class in the case of static methods.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by darthy

    I assummed that if there were several source code files in the same folder they could all be accessed from the source code file that had "main" in it.
    If the .class file that contains the main method resides in another directory you could say invoke a method in another class that resides in another directory. As long as you have access to the class first and then the method you should be fine.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Using JBuilder 8 if i right click on ProjectName.jpx there is an option to Add Files / Packages. What i did was create a simple class. Then use that option to add in the .java class X that contains the method that my class Sample wishes to invoke.

    The class with the method that is being invoke has to be compiled with the package package or it will fail to work.

    If you do not have access to the source code you can add the .class file into your project. When you double click on the .class file JBuilder will generate the stubs for the methods from the .class file with no implementation though.

    You can most likley add in the package(directory when the .class file is contained) but you will probably have to do some slasspath tinkering.
    Code:
    package Sample;
    
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author unascribed
     * @version 1.0
     */
    
    class Sample {
      public static void main(String[] args) {
    
      X x = new X();
      int i = x.getProduct();
      System.out.println(i);
      }
    }

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    102
    THanks for your help Guys but I think I have solved the problem. In the Default Project Properties I changed the root source path to the appropriate folder. This is what I was looking for.

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