Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Splat
  • 1 Post By Splat

Thread: How to import file from same directory?

  1. #1
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default How to import file from same directory?

    Hi,

    I am wondering if I can have more than one java file per folder (or does this break the java package scheme?). I'll give example that I am struggling with.

    In Test1.java
    Java Code:
    import test2;
    
    public class Test1
    {
    }
    In Test2.java
    Java Code:
    package test2;
    
    public class Test2
    {
    }
    When I try and compile the code Test2.java compiles but Test1.java does not.

    Java Code:
    C:\Projects\Java>javac Test2.java
    
    C:\Projects\Java>javac Test1.java
    Test1.java:1: '.' expected
    import test2;
                ^
    Test1.java:1: ';' expected
    import test2;
                 ^
    2 errors
    C:\Projects\Java>
    How can I have a java file access another java file class in the same folder?
    Okrim likes this.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    if it's part of the same package (same directory) then it doesn't need to be imported.

    Edit:

    example:
    \myproject\Test1.java
    \myproject\Test2.java

    Java Code:
    package myproject;
    ...
    Last edited by mrmatt1111; 10-13-2009 at 10:17 PM.
    My Hobby Project: LegacyClone

  3. #3
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default

    what if Test1 class accesses the class Test2?

    say like
    Java Code:
    public class Test1
    {
       Test2 x = new Test2();
    }

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    if both test1 and test2 belong to the same package they have access to each other.

    i would go through this tutorial: Packages
    My Hobby Project: LegacyClone

  5. #5
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default

    ok, this is what I mean.

    Inside my Test2.java file there is:
    Java Code:
    package myproject;
    
    public class Test2
    {
    }
    and inside my Test1.java file there is
    Java Code:
    package myproject;
    
    public class Test1
    {
    	Test2 x = new Test2();
    }
    When I try and compile I get:

    Java Code:
    C:\Projects\Java\Minimal>javac Test2.java
    
    C:\Projects\Java\Minimal>javac Test1.java
    Test1.java:5: cannot find symbol
    symbol  : class Test2
    location: class myproject.Test1
            Test2 x = new Test2();
            ^
    Test1.java:5: cannot find symbol
    symbol  : class Test2
    location: class myproject.Test1
            Test2 x = new Test2();
                          ^
    2 errors
    These are the files that are in this directory
    Java Code:
    C:\Projects\Java\Minimal>dir
     Volume in drive C has no label.
     Volume Serial Number is 8872-8339
    
     Directory of C:\Projects\Java\Minimal
    
    14/10/2009  02:58 p.m.    <DIR>          .
    14/10/2009  02:58 p.m.    <DIR>          ..
    14/10/2009  03:00 p.m.                71 Test1.java
    14/10/2009  03:03 p.m.               194 Test2.class
    14/10/2009  03:03 p.m.                46 Test2.java
                   3 File(s)            311 bytes
                   2 Dir(s)  22,921,027,584 bytes free
    I will try and work through the Packages tutorial as suggested (again) but I think I have been thru it before and I found it confusing. Anyway, maybe I missed something, so I will go thru it again.

  6. #6
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Smile Ok, I figured it out.

    I had to compile them both on the same command line for it to work. That is

    javac myproject/Test1.java myproject/Test2.java

    and then it would compile without errors. Then I had to be careful to run it from a directory level above like this:

    java myproject/Test1

    for it to work. Bit of a trap for players without an IDE. Anywayz thanks for your help.
    Okrim likes this.

Similar Threads

  1. Import error for csv file in outlook
    By nida in forum New To Java
    Replies: 5
    Last Post: 09-07-2012, 01:22 AM
  2. how to import a ejb bean in java file
    By vijayinani in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 06-30-2009, 10:05 PM
  3. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  4. how to import war file with tomcat?
    By makpandian in forum NetBeans
    Replies: 0
    Last Post: 12-31-2008, 12:00 PM
  5. [SOLVED] File chooser selecting file from directory...?
    By prabhurangan in forum AWT / Swing
    Replies: 12
    Last Post: 06-18-2008, 04:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •