Take the 2-minute tour ×

I'm not sure how to import a file from a directory above. That is, I have a setup like so

directory: MyProject
    directory: Other

Basically, Main.java is in "MyProject" and Other.java is in a folder inside the project's root folder. I can easily do

import Other.*;

to get those files available in Main, but how do I get Main.java to be visible to Other.java?

import ../Main.java

Obviously this doesn't work, but that's the general functionality I'm looking for. Any suggestions? I would prefer not having to use absolute paths. Thanks!

Edit: I meant import not include. Sorry. Been using C++ too much.

share|improve this question

5 Answers 5

up vote 0 down vote accepted

Java does not include files. You can however directly use classes using the simple name by using import statements.

Basically you need a file per (top level) class you define. This allows IDE's to rename compilation units, and do other refactorings. Besides that, it lets you easily add code at the right spot.

Java does use packages to create namespaces. Packages themselves are completely separate namespaces. Although the namespace seems to be a tree structure, in Java each package is actually not related to any other package. Hence you cannot use it as a folder structure, using .. is not allowed. This may change once "super packages" are introduced.

The Java import statement looks a lot like #include, but the name change is deliberate: instead of grabbing the file to make the definitions in that file known, it is simply an simply a statement to make it easier to refer to classes and interfaces. It has no other effect than having a shorter name to a class (or, for import static, constants and other static members).

Most of the time the top level classes are represented using a folder structure that reflects the package name. This makes it easy for IDE's and developers to find the file representing the class. It also makes it easy to put in version control. It is however not part of the Java specification itself; the location of Java source and classes is not defined. Earlier IBM IDE's actually stored Java source and classes in a database for instance; they did not use files at all. Newer IDE's such as Eclipse may use different source folders, e.g. one for Unit test files and one for the library itself.

So finally, the only way to include packages is by specifying the full package name, then a dot and then the class to import, or the * wildcard to import all classes of that package.

import java.util.Vector;
import java.util.*;

Most IDE's will create these import statements for you, possibly after you have chosen the right class to import (in case there are classes with the same name in different packages).

More information can be found in the Java Language Specification (Java 7 version).

In your case you have defined a Main class in the root or default package which is strongly discouraged. You can directly refer to Main without any import statement. The Other class is in the identically named Other package (using uppercase in package names is strongly discouraged as well). You can refer to it by using import Other.Other.

share|improve this answer
While there is nothing technically wrong with what you've stated, you haven't actually gone any way to actually answering the question "How can I include a file from the directory above in Java?". –  Matthew G Oct 1 '13 at 3:37
@MatthewG OK, rewrote answer, thanks for the heads up –  Maarten Bodewes - owlstead Oct 1 '13 at 18:56

include ???

Java doesn't have file source inclusion support, it rather use a naming conversions, so you should import the namespace (package) that you need in your source file.

You should define a package for your main class and then import it in the Other class .

share|improve this answer
Yes, I meant import, obviously. I'll try to make packages and see if I can get it to work this way. –  Sefu Feb 8 '12 at 22:53

the Main.java is in the default package, this is impossible to import from other (named) packages

put it in a package and import as normal

directory: MyProject
    directory: base
    directory: other

(also package names are lowercase normally)

share|improve this answer

if you have file outside of your project it means this file:

  • wouldn't be compiled by project
  • wouldn't get into jar
  • can't be used in runtime

so you really shouldn't include it.

Either move it into project, or include dependent project which contains that file.

Java is not like C++. You include by package name. So if toplevel file is in project AAA in folder src/aaa then you should include that project as dependent jar and refer to file as import aaa.Main

share|improve this answer
It is in the project. The main file is in the "root" directory of the project, and "Other" is a folder inside the main root directory - that is, the folder "Other" is right next to Main.java. I want to import Main.java (in root folder) into Other.java (inside the "Other" folder). –  Sefu Feb 8 '12 at 22:50

I think import Main; should just work.

You should read up java concepts package and classpath. Please look at the documentation here. The options that will work for you are sourcepath and classpath.

share|improve this answer

Your Answer


