How to run a java class with a jar in the classpath?

前端 未结 3 1805
轻奢々
轻奢々 2020-11-30 08:27

So, I can do this very well:

java mypackage.MyClass

if ./mypackage/MyClass.class exists. I can also happily do this:



        
相关标签:
3条回答
  • 2020-11-30 09:07

    Possibly :)

    # On Unix
    java -cp utilities.jar:. mypackage.MyClass
    
    # On Windows
    java -cp utilities.jar;. mypackage.MyClass
    

    Basically that's just including . (the current directory) on the classpath as well as the jar file.

    0 讨论(0)
  • 2020-11-30 09:17

    You should include the mypackage.MyClass into the CLASSPATH or the -cp parameter. For example:

    java -cp utilities.jar;myjar.jar mypackage.MyClass
    

    The path separator is ; on windows and : on Unix/Linux

    0 讨论(0)
  • 2020-11-30 09:20

    Try this if you're on Windows:

    java -cp .;utilities.jar mypackage.MyClass
    

    Or this if you're on Linux:

    java -cp .:utilities.jar mypackage.MyClass
    

    The current directory is not in the CLASSPATH by default when you specify a value for -cp.

    0 讨论(0)
提交回复
热议问题