error: package org.apache.commons.net.ntp does not exist - java

I downloaded commons-net-3.0.1-bin.zip file and extracted it to java lib folder. I have set the path to java bin folder and classpath=.;C:\Program Files\Java\jdk1.7.0_01\lib\commons-net-3.0.1.
commons-net-3.0.1 folder has commons-net-3.0.1 jar , commons-net-3.0.1-sources.jar and commons-net-examples-3.0.1.jar files.
In my program I imported org.apache.commons.net.ntp.* package, and it gave the "package doesn't exist" error.

Just adding the directory containing the JAR files isn't enough. Add the specific JAR file to the classpath instead, e.g.
classpath=.;C:\Program Files\Java\jdk1.7.0_01\lib\commons-net-3.0.1\commons-net-3.0.1.jar
Alternatively, use a classpath wildcard to add all JARs in a given directory. See How to use a wildcard in the classpath to add multiple jars?

check that package org.apache.commons.net.ntp.* presents in jar file
make sure that the jar file is in the class path of an application How to set Classpath

Related

classes of jar file cannot be imported in netbeans

I created a jar file from a bunch of java files. Folder structure was org/ax/redis. I used the command jar cvf jedis.jar org/*. Then I imported this jar file in my netbeans project. Then when I tried importing classes from it, by writing import org.ax.redis.*. However, netbeans shows error that no such package exists.
Now I opened another jar file of log4j to see how it is from inside. Only difference was in manifest file. It had a bunch of directives like Name: org/apache/log4j/. So I created a manifest file for my jar file by including Name: org/ax/redis/. Used this command to add manifest information in my jar jar cvfm jedis.jar META-INF/manifest.txt org/*. Still nothing works. Please help me
Jar files typically contain class files (the results of compilation) rather than source files (*.java). While some jar files may contain both, only the class files are available to compile or run against (e.g. using -cp library.jar).
So basically, before you build your jar file, you need to compile your code - and then include the class files in the jar file. If you include the source files as well (in the same directory structure) then some IDEs may be able to detect that, which can be useful.
Even If u don't generate .class file , the package still can be imported and it won't show any compilation error(If U don't specify any particular class) . However when U try to use a particular class function of that package , it will throw an error .

Java NoClassDefFoundError even when Jars in same folder

I have created a simple Java program (1 java file that contains the main() ), and I've included all Jar files in the same directory as the .class file. It is giving the NoClassDefFoundError message.
I've tried updating the Classpath to point to the directory, and I've also set "-cp ." to suggest that it look in the same directory as the .class file. However, the program still says it can't find the class def.
Any thoughts on what I should do?
Adding a folder tells java to look in that folder for .class files.
You can't reference .jar files via a folder name... Each .jar file's path must be listed on the CLASS_PATH explicitly.
this question's answer may be useful
When you try running a class from command line then a NoClassDefFound exception usualy means there is something wrong with your classpath.
You have explicitly define the classpath. You can do this in a few ways but the following way is the least prone to error:
Open a command shell and do the following:
1.) set classpath = {path to class files};{path to jars}
2.) java com.example.mainclass
Note: Even if your classes path and jar path is the same you need to specify them explicitly.
Note: If you have more then one jars place them in a folder say lib and add it to the classpath like: {path}/lib/* This will include all of the jar otherwise you have to specify them individually.
References: https://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html
Import the following package:
Import java.lang.NoClassDefFoundError;

default class path java

my program is in C:\Users\Programs\x.java
X.java is using some files that are in y.jar, z.jar.
y.jar and z.jar are in C:\Users\Programs folder.
(1)
C:Users\Programs> javac x.java
(2)
C:Users\Programs> javac -classpath y.jar:z.jar x.java
I am not getting any errors when I do (2) but when I do (1) I am getting errors. Isn't that classpath is set to current folder. If so why is it not seeing y.jar and z.jar.
Contents of a jar residing on the classpath are not automatically added to the classpath itself. A proper classpath in your case would be what you specified in case (2).
The classpath includes the current folder.
However, it does not include subfolders of the current folder.
If you try to use com.example.MyClass, Java will look for a file named com/example/MyClass.class directly inside of each folder in the classpath.
It does not look in subfolders or JARs inside of folders in the classpath.
Yes, classpath should indeed be set to the current folder by default. However, setting it to a given folder is not the same as setting it to a specific JAR file.

How to resolve classes with source src-jar library in Eclipse?

I have added this jar file to my project's build path under "libraries":
http://sunet.dl.sourceforge.net/project/jeplite/jeplite/jeplite-0.8.7/jeplite-0.8.7a-src.jar
and cannot get Eclipse to resolve ANY of the included classes.
Packages are visible, but no classes.
Error message is:
"JEP cannot be resolved to a type"
In eclipse, it should be on the build path if its a source tree .... Remember , a jar is just a glorified zip file, so be sure that the binaries are actually in your jar file. The steps to test are
1) unzip the jar file
2) if its source : then either try adding it to your build path, or just directly import the source folders into your project
3) if you see class files in the jar, then it should be okay to add them to "libraries"
This is only a jar containing the source code. You need a jar with the compiled classes in it. Try the jeplite-0.8.7a-bin.jar.

How can I specify dependencies in the manifest file and then to include it into my .jar file?

I generated .class files by the following command:
javac -cp \directoryName\external.jar myPackageDirectory\First.java myPackageDirectory\Second.java
I needed to use -cp during compilation and name of .jar file of an "external" library (external.jar) to be able to use this library from my code.
Using my .class files I have generated my .jar file in the following way:
jar cfm app.jar manifest.txt myPackageDirectory\*.class
manifest.txt contains just one line:
Main-Class: myPackageName.First
My problem is that I am not sure that I will be able to run my .jar file on other computers. I think so because during the compilation I specified the location of the .jar file of the external library. So, my .class files (included into the .jar file will try to find the .jar file of the external library in a specific directory and there is no guaranty that that the .jar file of the external library will be in the same directory as on the my computer.
I heard that the above problem can be solved by a
usage of a MANIFEST file that I
include in my own jar, and which will
list dependency locations
but I do not understand how it works. I do need to specify location of the "external.jar" at the compilation stage (otherwise the compiler complains).
First of all: you don't seem to compile a class called MainClass and all your .java files seem to be in a package, so I assume that MainClass is just a placeholder and you actually use the correct class name here.
You need to specify a Class-Path header that mentions your external .jar to your manifest.txt and deliver the .jar file together with your jar. You need to do this in addition to specifying the -cp at compile time.
Further to what Joachim Sauer (very correctly) says, there is a way to pack your dependency jars into the same jar as your own code. The programs that accomplish this create a super-main class and manipulate the classpath to find the dependent jars in your resulting jar.
Several programs can do this; one of them is called OneJar.

Categories