In Linux, How to Execute Java Jar File with External Jar Files

Compile and run java program with mutiple external jar files

If you want to add directory while contains all the required jars for running/ compiling your Java file you can use below command:

In Windows:

java -classpath ".;c:\lib*" MainClass

In UNIX/ Linux

java -classpath ".:/lib/*" MainClass

Note: In windows ; (semicolon) is the separator, while in UNIX/ Linux : (colon) is the separator for multiple jar for directory

. (dot) represents current directory

How to compile java project with external jar file in Linux terminal

You never use slashes, which are path delimiters, in a call to java (but to javac). If src is part of your package declaration - in this case the whole package declaration, which I bet it is not, you would, instead of:

 java -cp .:lib/jsoup-1.6.1.jar src/DayTradingStockBlog

use a dot:

 java -cp .:lib/jsoup-1.6.1.jar src.DayTradingStockBlog

But I guess it is just the place where you created the class, so the path belongs to the classpath:

 java -cp .:lib/jsoup-1.6.1.jar:./src DayTradingStockBlog

You aren't free to omit the path from the Class name, and append it to the classpath, or vice versa - it has to fit to your package declaration.

If you declare a package foo, (which has much more sense than src), your class name is no longer DayTradingStockBlog but foo.DayTradingStockBlog.

How to run Java program in terminal with external library JAR

You can do :

1) javac -cp /path/to/jar/file Myprogram.java

2) java -cp .:/path/to/jar/file Myprogram

So, lets suppose your current working directory in terminal is src/Report/

javac -cp src/external/myfile.jar Reporter.java

java -cp .:src/external/myfile.jar Reporter

Take a look here to setup Classpath

how to include external jar files while creating a jar file in linux?

There are two ways.

  1. Create a MANIFEST.MF that refers to the other jar in its classpath
  2. Whenever you run the program, make sure you include both jars in your classpath.

That said, I think you should go back and read a tutorial because you've made a few mistakes in your question.

  • "hello.java" is not a jar file as you've said. This is a java source file. It will be compiled into a class file. You can take this class file and put it in a jar file.
  • You can't name your manifest file "myManifest.txt" AFAIK. It has to be named something specific and be located in a specific place. Read a tutorial to see the details on that.

include external jar when running java -jar

Is there a reason why you are avoiding invoking the main class like

java -cp /usr/local/jar/foobar.jar:/some/other/path.jar com.your.main.classname

?

This type of invocation allows you to mix absolute paths with relative paths. Put this into a shell script or batch file to avoid having to actually type or remember the full classpath to simplify things.



Related Topics



Leave a reply



Submit