-1

First, I'm working in a linux environment. Second, I've got one jar file which contains all of my project's classes, including the main, in one directory. Third, I've got all of the project's external dependent jars in a sub-directory called lib.

The question is how do I run my program? I've tried:

java -classpath ".:/lib/*"  com.pom.ticketprocessing.main.TicketProcessing
java -classpath ".:/lib/*"  tp.com.pom.ticketprocessing.main.TicketProcessing
java -classpath ".:/lib/*"  tp.jar.com.pom.ticketprocessing.main.TicketProcessing
java -classpath ".:/lib/*"  TicketProcessing
java -classpath "tp.jar:/lib/*.jar"  TicketProcessing
java -classpath "./tp.jar:./lib/*.jar"  TicketProcessing

In each case I get the error: Error: Could not find or load main class TicketProcessing

So, how do I run this program? Thanks in advance.

4
  • 1) The 1st line looks the most realistic. Not knowing your directory structure and names it is hard to say more. 2) Show us the content of your "lib" directory and the content of ./lib/tp.jar.
    – mentallurg
    Commented Mar 27, 2020 at 23:52
  • Not sure why the content of the lib directory matters. There are just over 6 jar files in the lib directory. Those jars are libraries files like log4j and database access jars. The jar containing the main class and the application is called tp.jar but is not in the lib directory. I've got ./deploy/tp.jar and ./deploy/lib/*.jar The main class is called TicketProcessing.
    – Marc
    Commented Mar 28, 2020 at 21:51
  • None of the above. Put a Class-path: entry into the Manifest.MF listing all the libraries, and use java -jar.
    – user207421
    Commented Mar 29, 2020 at 2:08
  • ... and a Main-class: attribute, of course.
    – user207421
    Commented Mar 29, 2020 at 2:34

1 Answer 1

0

1) Since your own jar is not int the lib directory, you need both of them in classpath. That's why lines 1 - 4 are not correct. They are all missing tp.jar.

2) To refer jars, classpath should either use names of the jars without any wildcards, or use directory following one asterisk. It means you can use .../lib/abc.jar:.../lib/def.jar, or .../lib/*, but following will be just ignored: .../lib/*.jar.

3) Pay attention to using of /. When path element begins with /, it means path in the file system root, not in the current directory.

4) Usage of . is important only when calling scripts or executable binaries in the current directory. But when you provide path as an argument to Java, it is not necessary.

In your case you need following command:

java -classpath "tp.jar:lib/*" com.pom.ticketprocessing.main.TicketProcessing

Or if you like dots:

java -classpath "./tp.jar:./lib/*" com.pom.ticketprocessing.main.TicketProcessing
1
  • mentallurg, thank you very much for the explanation and the answer. It worked.
    – Marc
    Commented Mar 29, 2020 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.