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;
Related
libraries properties
For one of my java courses, I need to import a pre-compiled java class in netbeans. I can't use it apparently.
So my prof is using grasp, which I don't like as IDE so I'm using netbeans. She is useless for my problem, so the problem here is that she has compiled the Joueur.class from java libraries and she wanted that we use it in our project.
So I managed to transform the class in a jar file with jar.exe -cf, then I added it with right-clicking on libraries in my project in netbeans.
Netbeans listed i,so it apparently recognized it. But when I try to use methods from it, it's underlined. But I've read that netbeans will recognized the contents of the jar automatically so I should be able to use it without a problem.
Is there an import to do in the main package or something to use it? I can't find the information ...
global netbeans
The name of the package for the class has to match the directory path in the jar. So if you want to create the jar correctly for class x.y.MyClass, you need to create the directories for it, copy the class file to it and then use jar to create the jar file. Here are some example commands:
mkdir x
mkdir x\y
copy MyClass.class x\y
jar -cf MyJar.jar x
To verify, type the following:
jar -tf MyJar.jar
It should print out something like:
META-INF/
META-INF/MANIFEST.MF
x/y/
x/y/MyClass.class
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 am trying to create .jar files in java which can be used by including it in other projects. It doesn't contain any main but only classes and methods. I am using th following command to create .jar file from command line
jar cf firstjar.jar *
I am including the jar file in my project but still the class is not available in the project.
What's the problem in this.
My guess is that you've got a problem with the directory structure.
Suppose you have a class like this:
package org.foo;
public class Bar {}
There should be Bar.class flie in a directory org/foo. That structure has to be in the jar file as well - so you should go to the directory containing org and run:
jar cf firstjar.jar org
(Or whatever your top-level package name is.)
If you just include the classfiles from the foo directory then Java's expectations of where to find things will be invalid.
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