Results 1 to 6 of 6
- 10-13-2009, 10:08 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 21
- Rep Power
- 0
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
In Test2.javaJava Code:import test2; public class Test1 { }
When I try and compile the code Test2.java compiles but Test1.java does not.Java Code:package test2; public class Test2 { }
How can I have a java file access another java file class in the same folder?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>
- 10-13-2009, 10:13 PM #2
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
- 10-13-2009, 10:49 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 21
- Rep Power
- 0
what if Test1 class accesses the class Test2?
say like
Java Code:public class Test1 { Test2 x = new Test2(); }
- 10-13-2009, 11:12 PM #4
if both test1 and test2 belong to the same package they have access to each other.
i would go through this tutorial: PackagesMy Hobby Project: LegacyClone
- 10-14-2009, 03:46 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 21
- Rep Power
- 0
ok, this is what I mean.
Inside my Test2.java file there is:
and inside my Test1.java file there isJava Code:package myproject; public class Test2 { }
When I try and compile I get:Java Code:package myproject; public class Test1 { Test2 x = new Test2(); }
These are the files that are in this directoryJava 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
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.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
- 10-14-2009, 04:11 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 21
- Rep Power
- 0
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.
Similar Threads
-
Import error for csv file in outlook
By nida in forum New To JavaReplies: 5Last Post: 09-07-2012, 01:22 AM -
how to import a ejb bean in java file
By vijayinani in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 06-30-2009, 10:05 PM -
Read file from directory, update contents of the each file
By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:07 AM -
how to import war file with tomcat?
By makpandian in forum NetBeansReplies: 0Last Post: 12-31-2008, 11:00 AM -
[SOLVED] File chooser selecting file from directory...?
By prabhurangan in forum AWT / SwingReplies: 12Last Post: 06-18-2008, 04:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks