-4

Background: Running on an ec2 instance not eclipse.

I have a java program that has external Jar dependencies. I want to create an executable jar file for this java program (being able to package the external jar files into this jar file would be icing on the cake)

The problem is that for me to use the java cf or cmf command I need to have a .class file. But, when I try javac HKP.java it throws a bunch of errors saying "xyz does not exist or cannot recognize symbol" which I'm guessing is because it has no idea about the external jar dependencies.

I have tried doing javac -sourcepath /home/ec2-user/apps/HKP/lib/ HKP.java, but it throws the exact same errors as before.

EDIT I have tried javac -classpath file1.jar, file2.jar, file3.jar HKP.java -I have also tried this with ";" in between. both return errors saying -bash file2.jar command not found, so instead of continuing the argument, bash is recognizing the multiple jar files as commands.This is the initial problem I had that caused me to post here.

Any help is much appreciated. Thank you!

2 Answers2

1

If you want to compile a java source file into a class, you need to provide all classes which are used in that code on the classpath of the compiler.

If the classes come from (already compiled) external JARs, you typically do that by specifying javac -classpath with a list of JAR files, separated by : (on Linux). But you should really think about using an IDE or at least a maven build file (Which has the benefit, it can even download those JARs for you).

Both (Eclipse IDE and Maven build system) can also generate a ueber-jar with the external classes in there for easy execution.

Community
  • 1
  • 1
eckes
  • 10,103
  • 1
  • 59
  • 71
  • I used a pom file and maven to get those external jar files. It created a jar file for my java program as well. Except, I can't execute that jar file using java -jar command. I've modified my pom file according to instructions on Overflow, but have had no luck. That is why I am trying the manifest way. Thank you – SparkKafkaSetup Jan 16 '15 at 23:52
  • I have tried adding the external jar files individually as well putting them in a folder and then specifying that in the class path. Both have not worked. Thank you for the time and help! – SparkKafkaSetup Jan 16 '15 at 23:53
  • You should ask only one answer at a time. This answer is about how to compile Java. But I added a link on how to build it with Maven. – eckes Jan 16 '15 at 23:54
  • You need to specify them individually for `javac`. On Unix/Linux with a ; separator: `javac -classpath lib/file1.jar;lib/file2.jar`. But if you have a POM where those libs are added as dependencies of type "compile", then this all happens automatically. – eckes Jan 16 '15 at 23:56
  • I will keep that in mind for my next question. I will also try your rec. I was separating the jar files with "," instead of ";" before – SparkKafkaSetup Jan 16 '15 at 23:58
  • I tried javac -classpath lib/file1.jar;lib/file2.jar, but it wouldn't recognize the jar files after the first one. I would get something like this: javac: no source files Usage: javac use -help for a list of possible options -bash: kafka-clients-0.8.2-beta.jar: command not found -bash: metrics-core-2.2.0.jar: command not found -bash: slf4j-log4j12-1.5.6.jar: command not found – SparkKafkaSetup Jan 17 '15 at 00:07
  • I am sorry I was mixing things up, for Linux it is ":" not ";". You need to specify it without a blank. (and with the sources). – eckes Jan 17 '15 at 08:09
-2

try to Add the path in system variable and restart your computer.

Xmac
  • 53
  • 6