Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package does not exist error when package was added to classpath

Tags:

java

Note, linked solutions (ex. Fatal Error: Unable to find package java.lang in classpath or bootclasspath) do not work.

I get this error, but the package is imported (commons... .jar)

org.apache.commons.lang3.tuple //does not exist import
org.apache.commons.lang3.tuple.MutableTriple

Source code

import org.apache.commons.lang3.tuple.MutableTriple;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;

Build code:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/javac -target 1.8 -source 1.8 -classpath \ "../lib/commons-lang3-3.4.jar;../lib/httpclient-4.5.jar;../lib/httpcore-4.4.1.jar;../lib/org.json-20120521.jar;../lib/pdfbox-app-2.0.0-20150606.170503-1383.jar;../src/:../lib/commons-lang3-3.4-javadoc.jar;../lib/pdfbox-app-2.0.0-20150606.170503-1383-sources.jar" \ -d output \ ../src/com/tymaf/pdf/*.java

How to fix this problem?

like image 851
user1122069 Avatar asked Oct 27 '15 17:10

user1122069


2 Answers

Double check your classpath. Looks like you mixed delimiters ; and :.

Also instead of including jar with compiled classes (library itself). You've included java-docs and sources that are useless in classpath.

../src/:
../lib/commons-lang3-3.4-javadoc.jar;
../lib/pdfbox-app-2.0.0-20150606.170503-1383-sources.jar
like image 166
vvg Avatar answered Oct 11 '22 06:10

vvg


Here is my suggestion

How to compile and use .jar extension

.jar extension can be imported different ways depending on your environment and IDE.

Here how it work as native mode from console.

  1. Download the .jar.zip library from
    http://www.java2s.com/Code/Jar/c/Downloadcommonslang333jar.htm

  2. Create a folder in your working (project) directory call it libs

  3. Unzip the downloaded file and copy commons-lang3-3.3.jar to your working directory libs

  4. I have also created a class just for testing call it TheNewWork.java and added the 3 imports.

  5. Now from your working directory c:\projects for Compile:

    javac -classpath "/Projects/libs/commons-lang3-3.3.jar;" TheNewWork.java

  6. And for running it:

    java -classpath "/Projects/libs/commons-lang3-3.3.jar;" TheNewWork

If you have more than one .jar just add ; for Windows and : for Linux. Btw I use windows 10 cmder console and java jdk1.8.0_66. In other OS console you might need to put .:Projects...etc in stead of /Projects...etc. but the idea is the same.

UPDATE
In windows it is possible to set classpath like

set CLASSPATH=%CLASSPATH%;C:\Projects\libs\commons-lang3-3.3.jar

OR in Linux

export CLASSPATH=".:/Projects/libs/commons-lang3-3.3.jar"

Then you can run javac TheNewWork.java but it is personal taste to do it this or the other way. Some things similar is also possible to do in other OS.

Last thing, if you lazy and do neither want to write a full command line nor create a classpath, you could create a batch file with the full command line and run it that way in stead ;)

Some references:

  • http://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html
  • https://www3.ntu.edu.sg/home/ehchua/programming/howto/JDK_Howto.html
  • https://www.chilkatsoft.com/java-classpath-Windows.asp

I hope this solves your problem

Before the solution enter image description here

After the solution enter image description here


NOTE
In addition thanks to @MarkPeters notified me on my previous answer: Adding application dependencies directly to the JRE libs is not a good approach, as it makes the JRE suitable for running only one Java application, rather than being a generic runtime. Plus it would complicate whatever deployment the OP wants to do. lib/ext is made for extending the core Java APIs, as described here: docs.oracle.com/javase/tutorial/ext/basics/install.html. Not for normal application dependencies.

like image 36
Maytham Avatar answered Oct 11 '22 04:10

Maytham