Sefu
Sefu

Reputation: 2494

How can I include a file from the directory above in Java?

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

directory: MyProject
    Main.java
    directory: Other
        Other.java

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.

Upvotes: 1

Views: 24597

Answers (5)

Maarten Bodewes
Maarten Bodewes

Reputation: 94098

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 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.

Upvotes: 2

Sid Datta
Sid Datta

Reputation: 1109

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.

Upvotes: 0

ratchet freak
ratchet freak

Reputation: 48226

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
        Main.java
    directory: other
        Other.java

(also package names are lowercase normally)

Upvotes: 1

aleroot
aleroot

Reputation: 72686

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 .

Upvotes: 1

Sergey Grinev
Sergey Grinev

Reputation: 34528

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

Upvotes: 0

Related Questions