I want to add a package in java to another package, so I added that package to my build path in eclipse and copied the .jar file to lib folder. But still my program does not recognize that added package. Kindly help.
ok so i am assuming you want to import a class (or a set of classes) from a package into your class, correct?
if so, you need to add the jar to the buildPath and then add import statements in your class for the classes you want to include from that jar.
You can do a jar tvf to see the list of classes present in that jar
so if your class is called MyView, in MyView.java you need to do:
import com.a.b
where the jar you added to the buildPath contains the class com.a.b
Related
I have a JAR file that contains a class that I need for an assignment. I already added the JAR file using Properties > Libraries > Add JAR. I can see the file and the class inside it (it is in ) in the Projects side panel. But it won't let me create an object for it. It says symbol not found.
You should import the class which you want to use like import java.util.List;
I need to import a few class files into a java project in eclipse. I put the files in a folder and then add the folder to my project by right clicking the project, then select Properties > Java build path, then "Add External Class Folder".
Then I imported the class into the java file using an import statement:
import cla11.classname;
("cla11" is the folder name, "classname.class" is the name of the class file.)
However, the compiler doesn't allow the import (import cla11 cannot be resolved) and the classes contained in the class files are hence unusable in the project (... (class name) cannot be resolved to a type).
Note: I am aware that my question is almost the same as the one in this link: https://stackoverflow.com/questions/2477947/how-to-import-class-file-in-a-java-file#=
I used the method described in the answers, but the compiler does not allow it (as described).
(Since I am not yet allowed to comment, the only way I could think of is to ask the same question again.(Any other suggestions on what I could do to solve such problems would be welcome.))
Class' package name and your folder name must match. For example, if your class' full name is com.example.MyClass you need a directory structure like com/example/MyClass.class and import the root folder to the Eclipse.
Update
I do not know your actual needs. Like why do you even need to import an external .class file. So you may prefer to create a Jar file and add it as dependency. IMHO, This way it will be a lot easier to distribute your application.
You can create a jar file like this jar cf my-library.jar com/ after that, you can add Jar file as a dependency. For example if you are developing a web application you can simply drop your my-library.jar file to ${project-root}/WebContent/lib directory. Or if you developing a console application, you can simply add your my-library.jar file to your class-path.
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?
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.
In Eclipse, go to Project->Properties-Java Build Path where you can config the classpath which allow you to import.
I need to use a method that is located inside of a package in an external jar that i have loaded.
The problem is, this method relies on a class that is in the same jar but it is in the Default Package so i cannot import it into my project.
Is there a way to do this?
Using: Eclipse
If the class is already in the jar file and the jar file is on your classpath (in eclipse add the jar to build path, in command line add jar using -cp option) it should work fine no imports for the dependency class needed.
You cannot import the default package, since it has no importable path. You have to create a package and place the class in that.
Edit:
project << build path << add library << add external jars << add to build path
I have a class file to handle the GUI which is SampleGUI.class. I want to use that into my java project. I have created a folder named ClassFiles in root directory. I put the SampleGUI.class inside that folder. I went to project properties ->Libraries->Add Class Folder. Then i tried to call that class inside my new class created inside the project like
new SampleGUI ( length, width) ;. But it gives me the error "SampleGUI cannot be resolved to a type". Do i need to import anything? How do i procees with this situation?
I have create a Jar file using the command
jar cf jar-file input-file(s)
And I imported those jar files into my project and it all worked fine.
If SampleGUI is in a different package, you'll need to import it.
e.g.
import ClassFiles.SampleGUI;
where ClassFiles is the package name.
If SampleGUI is your own, and you have the .java source file, you can add SampleGUI to the ClassFiles package, if it isn't already, by adding the following line to the top of SampleGUI:
package ClassFiles;