-1

I am able to achieve the following two things already.

  1. Use ManifestFile to define classpath libraries and Main class as below.

    manifest-Version: 1.0

    Class-Path: lib/log4j-1.2.17.jar

    Main-Class: FQNofMainClass

  2. Pass the ClassPath libraries folder and MainClass from Command Line as below.

    (java -cp my.jar;lib_folder/* FQNofMainClass)

But now my scenario is to run the .jar file with Classpath libraries in command line and MainClass details inside Manifest File. How do I achieve this?

0

2 Answers 2

0

Make your JAR executable (to your OS and user) and then java -jar /path/to/your.jar

Sign up to request clarification or add additional context in comments.

2 Comments

If in case I wasn't clear before. My scenario is I want to create an java executable without dependencies defined in it. And I also should have Main class defined in manifest file. So the user who executes this will have option to provide those dependencies to classpath at runtime
You can create Jar without dependencies and let user supply it. I don't know if you can do jar and then provide custom classpath. without not using -jar
-1

Since you setup your jar to know that FQNofMainClass is the main class, you can run it using the -jar command-line option. But for this to work, you should also jar up all of the dependencies that your program relies on into the jar. This way you don't have any external dependencies and the jar can be delivered to users without any extra funny instructions or additional files to install.

java -jar my.jar

By "jarring up" the dependencies, I mean repackaging all of the .class files from the other jars you need into my.jar when you create it.

4 Comments

Steve, this is what I did initially. But, it is not able to find resources from specified path.
As the man page says: When you use the -jar option, the specified JAR file is the source of all user classes, and other class path settings are ignored. Although this isn't exact; Class-path entries in the manifest can still refer to other jars and directories.
right - apologies for the misstep. Jars are meant to be self-contained (like an .exe). If you bundle all of the dependent class files into your jar that will achieve what you want because it won't need any changes to the classpath at all.
That exactly is what the problem. In my scenario, I am not supposed to bundle the dependent class files from 3rd party jars. I want the user to pass those jars while execution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.