How to run a .class file that is part of a package from cmd?

后端 未结 12 1245
梦如初夏
梦如初夏 2020-12-01 07:11

I keep getting errors when I make my .class part of a package and try to run it from cmd.

Here\'s the code that works after using jav

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 07:58

    The syntax is:

    java -classpath /path/to/package-folder .
    

    So you may try: java com.HelloWorld which would expect com/HelloWorld.class file to be present as classpath by default points to the current directory (if not specified).

    In case you're in different folder, try specifying classpath:

    $ CLASSPATH=/path/to/package-folder/ java com.HelloWorld
    Hello World!
    $ java -cp /path/to/package-folder/ com.HelloWorld
    Hello World!
    $ cd /path/to/package-folder/ && java com.HelloWorld
    Hello World!
    

    For further explanation, check: How do I run Java .class files?

提交回复
热议问题