5
votes

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?

2
Could you please format your question correctly? Why do you have a backslash between -classpath and the rest of the command? Why do you have quotes around the classpath? From which directory do you execute that command, and what is the directory layout?JB Nizet
how are you importing the external library to your project? what process you did. Your questions is very ambiguous so please make it clear.Muhammad
it would be helpful to format the 'build code'. This seem to be multiple commands in sequence and the many `\` are probably line joiners that can be removed here for better readability.CoronA
@user1122069 "Technically I got that from the other answer." This shows that I cared about your question and spend some time to give you the hole picture of solving it. but it seems you do not appreciate that ;)maytham-ɯɐɥʇʎɐɯ

2 Answers

5
votes

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
2
votes

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:

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.