0

I have a java class that uses some external lib. It runs a powershell script and does some stuff after that. I want to run the script with Jenkinsfile, and local env via IntelliJ, so the command should be universal for Linux and Windows. How can I execute a command line to run it?

Some things that I've tried: I have created a artifact and tried to execute it by java -jar but nothing java -jar ./out/artifacts/test_jar/test.jar I got this

Error: Could not find or load main class APITestRunner
Caused by: java.lang.ClassNotFoundException: APITestRunner

Also I have tried with javac but got an error error: package my.package does not exist I have a big root so whould be nice to see any -cp exapmles. Is that doable? :D

  • There's no real mystery about dependencies. It's just a question of making sure they are all in the classpath. One caution: if you do `java -jar foo.jar`, you are telling java that *all* dependencies are in the jar mentioned. If you have multiple jars, you must not use `-jar` but need to use `-cp`. Your error message suggests that you made an executable jar OK, but specified your main class incorrectly – g00se Apr 14 '23 at 14:42
  • @g00se I didn't get you. How can I add all dependencies to my .jar if this is the external library? Also with -cp, I do not have a path to the library. Or did I do something wrong? – Serega Ilushenko Apr 14 '23 at 16:34
  • *How can I add all dependencies to my .jar if this is the external library?* You would use a so-called 'fat jar'. *Also with -cp, I do not have a path to the library* Sorry, I don't understand. Of course, you *must* have a path, and specify it – g00se Apr 14 '23 at 18:03

1 Answers1

0

Extract your test.jar and open META-INF/MANIFEST.MF in a text editor. If there is an entry Class-Path in the MANIFEST.MF file, make sure all the dependencies listed in the Class-Path entry are available in the locations specified in the entry (Class-Path entries are relative to the location of test.jar). With all dependencies in place, you should be able to run your application with java -jar [path to test.jar].

If there is no Class-Path entry, you need to track down the dependencies of your jar in some other way. Put all those dependencies in a directory of your choice. (A lib subdirectory in the directory that contains the test.jar is usually considered a good choice.) Then run your application using

java -cp <classpath> <main class name>

E.g. suppose there are two dependencies dep1.jar and dep2.jar and the qualified class name of your main class is com.yourcompany.Main, then the command for Linux would be

java -cp test.jar:lib/dep1.jar:lib/dep2.jar com.yourcompany.Main

and for Windows

java -cp test.jar;lib\dep1.jar;lib\dep2.jar com.yourcompany.Main

Both examples assume that you run the java command in the directory that contains test.jar.

If you need to use the same command on Linux and Windows, you need to rebundle test.jar such that its META-INF/MANIFEST.MF has a Class-Path entry (and a Main-Class entry).

Thomas Behr
  • 761
  • 4
  • 10
  • Ok, " Put all those dependencies in a directory of your choice." I cannot do this cuz this is the external library. Should I download it or...? – Serega Ilushenko Apr 14 '23 at 16:54
  • @SeregaIlushenko What do you mean by "external library" and why does this prevent you from getting the dependencies of your java class? In any case, you cannot run a Java application without its dependencies. That is why they are called dependencies. – Thomas Behr Apr 17 '23 at 11:41