How to reference a class in an external JAR in Eclipse? - java

I would like to reference a class Bag in a JAR file, but Eclipse is telling me that Bag cannot be resolved to a type. I have added the JAR in which Bag is defined to the classpath for the project, but the error is still there. What am I doing wrong?

I think you can't do that, because the Bag class in algs4.jar is inside the default package.
Before J2SE 1.4, we still can import classes from the default package using a syntax like this:
import Unfinished;
But from J2SE 1.5, that's no longer allowed. So if we want to access a default package class from within a packaged class requires moving the default package class into a package of its own. Read here for more detail explanation :
How to access java-classes in the default-package?
Some options you can choose :
Access the class via reflection or some other indirect method. But it is a little bit hard, something like this :
Class fooClass = Class.forName("FooBar");
Method fooMethod = fooClass.getMethod("fooMethod", new Class[] { String.class });
String fooReturned = fooMethod.invoke(fooClass.newInstance(), new String("I did it"));
If you own the source code of that jar library, you need to put it in properly package and wrap it again as a new jar library.

You may need to either fully qualify the Bag class, or import it.

Related

cannot resolved to a type error in java

I'm new in java, please help me to understand this.
I can see there is ReadHtml class and defined with one public method. But when i put this code in ecplise, it shows red mark under WebClient with tag that "this cannot resolved to a type". May I know what does it mean? Gone through all about method definition but couldn't find any remedy to understand this.
Can I get any help ?
public class ReadHtml {
public static LinkedList<String> readJacksonCounty(String urlName, String pStartDate,String pFinishDate)
{
LinkedList<String> xmlListReturn=new LinkedList<String>();
System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "error");
final WebClient webClient1 = new WebClient(BrowserVersion.CHROME);
webClient1.setJavaScriptTimeout(60000);
webClient1.getCookieManager().setCookiesEnabled(true);//enable cookies
webClient1.getCache().clear();
You are missing an import of this library:
import com.gargoylesoftware.htmlunit.WebClient;
Add this to the top of your file (and read dsp_user's comment for future reference).
Basically "...cannot be resolved to a type" means that type is not available on the class path. If you're just using eclipse refere to How to import a jar in Eclipse.
If you already added the needed jar onto your class path, you are missing the import statement. Imports just make it so that you dont have to use a class's fully qualified name. (you can type
MyClass myClass;
as opposed to
com.some.package.MyClass myClass;
if you add
import com.some.package.MyClass;
at the top of your file.
Note that if you want to build a jar from your project you'll need some kind of build tool. If you choose to use Maven, which is very common, just read any tutorial on how to get started and manage dependencies.

Passing a customized object to Scala template

I have an object profileModel in my profile package and my profile.scala.html file have following code
#(model: ProfileModel)
when I compiles, it is giving an error recursive value model needs type
But when I moved this class to models with my application.conf as
ebean.default="models.*"
it works.
My guess is scala compiler automatically adds models.* to class path at the time of compilation
Is there a way to make this work without moving the class back to models package ?
I am using play 2.2.1 built with Scala 2.10.2
If I understand you right, if your ProfileModel exists in profile package correct declaration in the view should be:
#(myProfile: profile.ProfileModel)
And 'yes', Play imports automatically all models and controllers (and also other well known types), but if you want to use any type in custom package (or ie. imported lib) you need to use full qualified path to it.

Importing Libraries Java

So I have two Jars I want to use in my project from here http://www.jhlabs.com/ip/filters/index.html
I added both CheckFilter and MarbleFilter to my class path. But when I do
CheckFilter();
It says I have to create a method CheckFilter()
I'm pretty sure that's the method I need to call to use that effect. But when I try any of the other methods in the library Jar it still gives me the same thing.
I have no experience with importing/using external libraries. Any help would be great.
checkFilter = new CheckFilter();
CheckFilter();
I tried above and it says I need to create a local variable checkFilter
How are you writing up the code. I will suggest to use eclipse IDE, it will make your tasks simple
If you are using eclipse. You need to do import the jar Filters.jar to your build path
which as you mentioned you downloaded from JHLabs Download page
I found Filters.jar inside dist directory.
Then you will be able to import the class or package
import com.jhlabs.image.*;
OR
import com.jhlabs.image.CheckFilter;
After importing the class or package you will be able to create object to it by
CheckFilter checkFilter = new CheckFilter();
In case you are totally new you can take help from people over IRC or chat and get going.
Someone would be able to quickly help you out
----==----==----==----==----==----==----==----==----
Read your comments and Question again.
You are totally missing the point. If you call to CheckFilter() directly without invoking new keyword, compiler will consider you are trying to access a method which is inside the class you are writing up. and give you error.
As I mentioned above. Your are trying to accessing Instance variable for the class without declaring it. Either do
CheckFilter checkFilter;
before you access checkFilter variable or directly instantiate the class the way I mentioned.
Seems to me you are missing a log of points :D
Methods don't exist without a class. That is probably the constructor to a class. Use
CheckFilter checkFilter = new CheckFilter();
instead. Then call methods on checkFilter.
From #andy-thomas in this similar question
The import statement imports classes from the jar file, not the jar file itself.
An import statement of the form:
import stdlib.*;
will import all the classes in the package stdlib.
Oracle provides this tutorial on importing.
This statement
CheckFilter();
Tries to call a method CheckFilter defined in your class, which is not the case. If this is a utility function, this may be a static method, in which case you can call it like this
ClassName.CheckFilter(); // replace ClassName with the class containing this function
If not, then you may have to instantiate an object
ClassName obj = new ClassName();
obj.CheckFilter();
or skip the object variable
new ClassName().CheckFilter(); // Not prefered
To add jar files, right click on your project in "Package Explorer", go to "Configure Build Path" and then to "Add External Jars".
Set the jar (libraries) into your classpath and use import statements in your java code to include the required Classes.

Java Adding External Library failed when Self-defined Package Exists

Recently I have encountered some problem which seems a little strange to me.
In order to use some predefined class, I imported two .jar files say foo.jar and bar.jar(Both were written by others)
And my source code is like the following:
package jerry.deque
public class Deque {
.....
.....
Foo item = new Foo(); //Already defined in the foo.jar
.....
}
I added the external library exactly as what How to Import a Jar in Eclipse
did. But when I tried to use the class defined in foo.jar Eclipse shows me that "Foo can't be resolved to a type".
I spent a lot of time to fix this problem and finally succeeded after I removed
the clause: "package jerry.deque" at the beginning of my class file.
I think this is weird because just a few days ago when I was doing some Android development, I followed the same way to add a Twitter API library. And it works fine even when I declared "package jerry.search_twittes" at the beginning of my .java
file. I'm confused by this problem and couldn't figure out what's going wrong. Could someone help me to explain it in detail? Thanks very much.
Check that Foo is same package as Deque class. If they are not same
package, you need to import Foo class in Deque class.
For example,
package jerry.deque;
import packagename.foo; // packagename.foo
public class Deque {
.....
.....
Foo item = new Foo(); //Already defined in the foo.jar
.....
}
Added Explanation
I want you to check access modifier of Foo class carefully.
There are 2 access level for top level(class) access control . These
are public, or package-private (no explicit modifier).
Your Foo class is under default package(not specified package)and may be no explicit access modifier. Hope so! Then, all classes under default package can access to Foo class. That's why when you remove package jerry.deque clause, it works.
Similarly, I want you to check Android development java code in which it works fine even when you declared "package jerry.search_twittes". In that case, classes inside Twitter API library's access modifier is public.So you can access it from anywhere.
For more information you can read this.Is this information helpful???
Foo is in default package. Classes from default package cannot be imported directly.
So when you remove the package declaration in your code, you don't get the error.
You can look for reflection api or write a proxy in the default package for that class.

Why don't I have to import a class I just made to use it in my main class? (Java)

I am currently learning Java using the Deitel's book Java How to Program 8th edition (early objects version).
I am on the chapter on creating classes and methods.
However, I got really confused by the example provided there because it consists of two separate .java files and when one of them uses a method from the other one, it did not import the class. It just created an object of that class from the other .java file without importing it first.
How does that work? Why don't I need to import it?
Here is the code from the book (I removed most comments, to save typing space/time...):
.java class:
//GradeBook.java
public class GradeBook
{
public void displayMessage()
{
System.out.printf( "Welcome to the grade book!" );
}
}
The main .java file:
//GradeBookTest.java
public class GradeBookTest
{
public static void main( String[] args)
{
GradeBook myGradeBook = new GradeBook();
myGradeBook.displayMessage();
}
}
I thought I had to write
import GradeBook.java;
or something like that.
How does the compiler know where GradeBook class and its methods are found and how does it know if it exists at all if we dont import that class?
I did lots of Googling but found no answer.
I am new to programming so please tolerate my newbie question.
Thank you in advance.
It is because both are in same package(folder). They are automatically imported no need to write import statement for that.
You don't have to import classes that are in the same package as the current class.
Also, note that GradeBook.java is the name of the file. The (simple) name of the class is GradeBook. Every class should be in a package. If it is in the package com.foo.bar, the class name is com.foo.bar.GradeBook, and this is the name you must use when importing this class.
Read http://download.oracle.com/javase/tutorial/java/package/packages.html to learn more about packages.
The classes located in the same package do not have to be imported, as they are visible to each other. You simply import a class that is in another package:
import java.util.ArrayList;
Note that you are not importing the file, but the class.
It's all about packages. You are trying to use a class from the default package which does not need explicit import of the java file, ie GradeBook inside GradeBookTest
Here is where you can start with learning about packages :
Java Package Tutorial
and :
Creating and Using Packages
Java doesn't use includes the way C does. Instead java uses a concept called the classpath, a list of resources containing java classes. The JVM can access any class on the classpath by name so if you can extend classes and refer to types simply by declaring them.
From: Include one java file in another java file
Imports are for importing classes that are in a different package. Since you didn't declare a package for either they are both put in the default package. The compiler can find it because the class lives in the same directory.
You don't have to import classes which are in the same package.
Well, classes in the same package are automatically imported.
They're in the same package. This tutorial will do more justice than I will.

Categories

Resources