java import static org.mockito not working - java

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.

Related

JAVA Importing class from external library

I am trying to use the SimpleRegression class (link).
I first imported the .jar file as external jar into my project and I can see it is imported.
When I try :
import org.apache.commons.math3.stat.regression.*;
I have no error message, so the jar file is correctly imported,
but when I import specifically the class I need:
import org.apache.commons.math3.stat.regression.SimpleRegression;
I get the message that SimpleRegression cannot be resolved.
Can somebody tell me what I did wrong?
I downloaded the package from here .
thanks for your help
If your are using NetBeans the way to add a library to your class path is to go to
File-Open Project-Select Your Project Name-Right Click on Libraries-Click Add Jar/Folder and then select your jar file. Then you must wait for it to do it's background scan before your run with the new imports. It is possible that an error occurred when you imported the jar into your project so try removing the jar now, then follow the steps above to add it to your class path. If it still doesn't work after that the jar file may be corrupted.

how to import source code into Eclipse from a jar file?

I got a jar file that is packaged with source code in it. I want to load it as a top-level project and start working on the source code. How can I do that in Eclipse?
For example: if I have helloworld.jar, when I import this jar. I want to have the project name as helloworld, with all the packages and src inside it.
I do not want that code on build path.but on my IDE to work with.
Create a directory named "helloworld" and unpack the JAR into that directory:
mkdir helloworld
cd helloworld
jar xvf ../helloworld.jar
You can then create a project from existing sources and start operating on that. I don't think Eclipse will let you create a project and change files directly in the JAR.
#eagertoLearn Yes. Eclipse does this in a very easy way than suggest by #Erik Gillespie.
Use Import from archive file feature in eclipse. Refer this post https://stackoverflow.com/a/11983201/1391924
Supported archive file types:
*.jar
*.zip
*.tar
*.tar.gz
*tgz
Right click on the .jar and choose open with -> winrar (or another archiver) and then click extract.
From there you can import the folder in Eclipse as a new project.
I was able to import the jar file contents using the following steps:
Create a new java project
Import jar file using file>import>general>archive file option
Move the folder containing source code into the src folder using drag and drop
Run the class containing the main method

Java Import From External Jar Default Package

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

Can't import packages from jsqlparser library

I added the jar jsqlparser-0.7.0 to my Netbeans IDE (right clic on libraries / Add Jar file) but i still can't use its packages. what could be the reason . The library is created on 2011 it could be it is not supported by JDK7?
The JAR file "jsqlparser-0.7.0.jar" is not a regular Java library. It is a simple zipped file containing several artifacts: a source and a documentation directory, some testfiles, and - what you need - another JAR called "jsqlparser.jar" in a lib directory.
This JAR is the "real library" that you must add to your project. Additionally, all packages inside this library start with "net.sf.jsqlparser".
Just put this:
import jsqlparser.*;

jars added to WEB-INF/lib doesn't get recognized when I try to import them : says the package doesn't exist

I have added org.apache.commons.fileupload and org.apache.commons.io package into the WEB-INF/lib directory of my project based on google appengine. But when I try importing in the servlet files the compiler/IDE gives an error that this package doesn't exist. Why is that ?
The jar files added :
What can be the reason I am getting this error ? What should I do to solve this problem ?
You might also have to add them to your project's classpath.
for eclipse:
Right click the jar, select build path and add it to the build path, then try again.
for netbeans:
in the project properties window click libraries in the left panel. In the right panel add it to the compile classpath
General solution , whenever you get package does not exist then there are 2 things, 1- Its not at all present 2) its present but still the error is thrown.
The solution to this is to just add the jar to the classpath [so that your app finds it during compilation,execution)
I would suggest you to try Maven . Maven is a nice way by which you can organize this in a systematic way.
If the IDE gives the problem then update the jars in IDE project class path :)

Categories