0

I am trying to compile a .class from my .java source code, using a JAR I have downloaded locally to a download directory in my project directory tree. I am using this JAR.

The .jar file exists where I expect it to be:

$ file download/amazon-sqs-java-messaging-lib-2.1.1.jar
download/amazon-sqs-java-messaging-lib-2.1.1.jar: Zip archive data, at least v1.0 to extract

The .jar file contains the class that javac fails to find:

$ jar -tf download/amazon-sqs-java-messaging-lib-2.1.1.jar | grep SQSConnectionFactory.class
com/amazon/sqs/javamessaging/SQSConnectionFactory.class

Here is how I compile, and the compiler error I get (I use : in the classpath since I am compiling on Linux):

$ javac -g -encoding utf8 -Xlint:deprecation -classpath '.:download/:download/\*' com/redacted/detectionengine/HandlerFirehose.java
./com/redacted/detectionengine/SqsListenerRunnable.java:3: error: package com.amazon.sqs.javamessaging does not exist
import com.amazon.sqs.javamessaging.SQSConnectionFactory;
                                   ^

I am using javac version 11.0.15.

Since the class com/amazon/sqs/javamessaging/SQSConnectionFactory.class exists in the JAR in the download directory, I do not understand why I get this compiler error. As far as I can tell, the classpath argument I provide to javac is correct.

Edit:

When I try g00se's suggestion, I get this:

$ javac -g -encoding utf8 -Xlint:deprecation -classpath '.:download/amazon-sqs-java-messaging-lib-2.1.1.jar' com/redacted/detectionengine/HandlerFirehose.java
./com/redacted/detectionengine/SqsListenerRunnable.java:3: error: cannot access SQSConnectionFactory
import com.amazon.sqs.javamessaging.SQSConnectionFactory;
                                   ^
  bad class file: download/amazon-sqs-java-messaging-lib-2.1.1.jar(/com/amazon/sqs/javamessaging/SQSConnectionFactory.class)
    class file has wrong version 61.0, should be 55.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.
blurfus
  • 13,485
  • 8
  • 55
  • 61
Shane Bishop
  • 3,905
  • 4
  • 17
  • 47
  • 2
    `javac -g -encoding utf8 -Xlint:deprecation -classpath '.:download/amazon-sqs-java-messaging-lib-2.1.1.jar' com/redacted/detectionengine/HandlerFirehose.java` should be what you need – g00se Jun 02 '23 at 15:59
  • Yes, the OPs `-classpath` parameter syntax looks incorrect – blurfus Jun 02 '23 at 16:04
  • @g00se please see my edit, I still get a different error for the same import – Shane Bishop Jun 02 '23 at 16:05
  • 2
    You're using 11 but you've got a jar compiled with 17. – g00se Jun 02 '23 at 16:09
  • 1
    Looks like you are trying to code using SQS in Java. Why are you trying to work with the Java SDK like this? Its much easier to use Maven and specify your dependencies using a POM file. – smac2020 Jun 02 '23 at 16:36
  • 1
    You really, really need to use a build tool such as [maven](https://maven.apache.org) or [gradle](https://gradle.org/). You may get to the point where you can compile - and then you'll need another jar dependency. Are you using an IDE such as Eclipse or IntelliJ? – stdunbar Jun 02 '23 at 17:04
  • As @g00se pointed out, the second error is a different issue; related to different java versions. It should really be a second question but it would get voted down for being a duplicate. Search [so] for a similar error and you'll find the answers to the JDK version issue – blurfus Jun 02 '23 at 17:08
  • There are two reasons why I am invoking javac directly rather than using Gradle or Maven. (A) My team has not had time to migrate from invoking javac directly to using Gradle or Maven. (B) I want to know for erudite purposes. Arguably there is also (C), what if one day in the future I find myself building the next Java build tool, and I need to know how to use the low-level Java tools correctly? – Shane Bishop Jun 02 '23 at 17:21
  • As for working around the JAR not being compatible with Java 11, I found I could compile with an older version of the JAR, but hardcoding the single JAR file in the classpath does not work, since there are several other JARs I need to compile against. My original classpath argument is based on [this answer](https://stackoverflow.com/a/25390904/8593689) to a question about how to tell javac about *all* the JARs it needs to be aware of to compile the project (specifically the third example). What would be the correct classpath argument to use every JAR in the `download` directory? – Shane Bishop Jun 02 '23 at 17:24
  • That's a terrible idea. You need to start thinking on how to make repeatable builds. If you are going to work with jar files directly create a project directory and put the needed jars in a lib subdirectory. Then list them all one by one in the classpath argument of javac.: `-cp lib/a.jar:lib/b.jar:...` – aled Jun 02 '23 at 20:14

0 Answers0