Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 835
gggggggg5555 Avatar asked Sep 12 '26 03:09

gggggggg5555


1 Answers

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.

like image 147
Alexandre Santos Avatar answered Sep 13 '26 18:09

Alexandre Santos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!