Java Import From External Jar Default Package - java

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

Related

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?
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.

java import static org.mockito not working

I downloaded the mockito-all-1.9.5.jar and put it in ~/java. Then I set the classpath -
export CLASSPATH=~/java
I am getting this error -
a.java:3: package org does not exist
import static org.mockito.*;
Any ideas what can be the cause?
Add the jar to your classpath:
export CLASSPATH=~/java/mockito-all-1.9.5.jar
The class files are not in your ~/java but in the jar file.
Why do you want to put the library in your classpath ?
If there is no special reason, the good way to add a jar is to put the jar in your project, then from eclispe, click right on the jar and click on buildPath->Add to build path.

Packages not found while compiling java

I'm trying to compile my java file to class.
It is a plugin to a much larger program and it runs fine from eclipse when running from source.
I work in Linux, and in shell I type the following javac MyPlugin.java
This is just some of the output:
MyPlugin.java:11: package javolution.util does not exist
import javolution.util.FastList;
^
MyPlugin.java:12: package javolution.util does not exist
import javolution.util.FastMap;
^
MyPlugin.java:14: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
^
MyPlugin.java:15: package org.jwebsocket.api does not exist
import org.jwebsocket.api.PluginConfiguration;
^
My assumption is that the rest of the errors are caused because the imports cannot be reached.
Do I need to add something to the original command to have these imports included?
You are missing jars from the following projects:
http://javolution.org/
http://logging.apache.org/log4j/1.2/
http://jwebsocket.org/
You can download the missing JARs and include them in your compilation classpath.
Alternatively, you could let maven manage the download and compilation of your application.
Update: Gathering all external jars together in one location
To get all your external libraries in one place you could create a runnable JAR in eclipse using:
Export...->Java->Runnable JAR File
Enter your_temp_jar.jar and click
Copy required libraries into a sub-folder next to the generated JAR
After clicking Finish all the required jars will be in a folder called your_temp_jar_lib.
You should specify the jar files which contains following classes in your classpath.
javolution.util.FastList
javolution.util.FastMap
org.apache.log4j.Logger
org.jwebsocket.api.PluginConfiguration
you can use -cp to specify classpath of jars.
Typing javac at the command line is not a viable or scalable way to build java code. While you could use -cp to javac to add the required dependencies to this compilation, you'd be far better served by learning ant or maven.

how can add new package in java to another package?

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

Access external class files in java using eclipse

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;

Categories