gggggggg5555
gggggggg5555

Reputation: 85

How do I know the path for Java import?

I'm trying to learn Java ( I know php ) but I don't know what path the included classes have. For example, I create a new directory in Eclipse (in the package) and drag there a class from other project. When I try to import it, it cannot find the class. Even if I don't have any dirs and the class is directly in the package, using import package.classname doesn't work...

I must be missing something but googling doesn't show me any replies.

How do I get the class path? Is it somewhere in the properties?

Upvotes: 3

Views: 14412

Answers (2)

sendon1982
sendon1982

Reputation: 11244

In Eclipse, go to Project->Properties-Java Build Path where you can config the classpath which allow you to import.

Upvotes: 0

Alexandre Santos
Alexandre Santos

Reputation: 8338

Java has the concept of a classpath: a path where all classes should be found.

You can get the existing classpath with this code:

 System.getProperty("java.class.path")

If you run java from the command line then you have to set your own classpath with your classes.

From the classpath, use the package to find the class. For example, if the classpath is ".", which is the current folder, and you have a class called A, which is in the package com.yourcompany, then you will find the class under ./com/yourcompany/A.class

On the example you gave, go to the terminal and look for the "bin" folder and you will see all classes. However, if you want to add a new class from another project to your project, then there are simpler ways. You can simply open your build path in Eclipse and add the class from the other project onto your project.

Another way is to create a jar from the other project and add the jar to your project.

Upvotes: 4

Related Questions