-2

I have a Java class named Client with main(String[] args).

main uses classes defined in a jar called lib.jar. How can I run the program from the command line passing command line argument to main?

I tried:

java -cp lib.jar Client arg1 arg2 

and got this error:

Error: Could not find or load main class Client

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
  • Is it `main()` or is it `main(String[] args)`? – childofsoong Nov 01 '16 at 17:18
  • 2
    "it did not work" is *never* enough information. What you've shown is indeed how you pass command line arguments. So what happened? If you got a class loading exception, then perhaps you wanted `java -cp lib.jar;.` (Windows) or `java -cp lib.jar:.` (Unix) so that the current directory is on the classpath as well? Without knowing what happened, we have no way of helping you - we're just guessing. – Jon Skeet Nov 01 '16 at 17:18
  • @childofsoong `main(String[] args)` – Ali Bdeir Nov 01 '16 at 17:20
  • In future, please get all the relevant information *before* you ask a question, ideally posting a [mcve]. Please bear in mind that the aim of Stack Overflow is to create a repository of high quality questions and answers. Without enough information, this is not a high quality question. – Jon Skeet Nov 01 '16 at 17:26
  • Is the main class `Client` or is it something more like `com.example.www.SomePackage.Client`, perhaps? – childofsoong Nov 01 '16 at 17:36
  • OK, I have edited the question, no need to down vote any more. Thank you. – Ali Bdeir Nov 01 '16 at 17:39
  • @childofsoong the method main is in a class that is in the default package. – Ali Bdeir Nov 01 '16 at 17:40
  • Possible duplicate of [What does "Could not find or load main class" mean?](http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) – childofsoong Nov 01 '16 at 17:41
  • @JonSkeet I have edited my question to the best of my ability. This problem is driving me crazy, any help? – Ali Bdeir Nov 01 '16 at 18:06
  • Right, it's a classpath problem, as I suggested in my second comment. Your classpath currently *only* consists of `lib.jar`, which doesn't contain `Client`. – Jon Skeet Nov 01 '16 at 18:07
  • Note that you should also read the [mcve] page for how to *really* provide a minimal but complete example. – Jon Skeet Nov 01 '16 at 18:10

2 Answers2

0

java -jar fileName.jar arg1 arg2

Assuming you exported the project as a .jar.

public static void main(String[] args) {
    System.out.println(args[0]);
}

Make sure you do the main method correct, as well.

-1

java -cp lib.jar packageName.className arg1 arg2

Mansi Shah
  • 227
  • 2
  • 6
  • @AbAppletic: Well it is, because we don't know whether the problem is that you didn't package-qualify the class name. This is the sort of thing that posting a [mcve] would help to avoid. Without enough information, you're just inviting other people's to waste time guessing. – Jon Skeet Nov 01 '16 at 17:28
  • @AbAppletic, Yes you didn't specify in the question that you have specified the package name or not. – Mansi Shah Nov 01 '16 at 17:34