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

后端 未结 12 1178
梦如初夏
梦如初夏 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:52

    There could be such a problem, when you are executing 'java' command directly from folder of your '*.class' files. This command should be executed from project 'parent' directory. All is well explained in the following article:

    http://javarevisited.blogspot.de/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html

    0 讨论(0)
  • 2020-12-01 07:55

    You should compile it first by typing this command in CMD, for exemple your file is in c:\ directory :

    C:\com>javac HelloWorld.java
    

    After that you can run the result by typing:

    c:\com>java HelloWorld
    
    0 讨论(0)
  • 2020-12-01 07:56

    Packages are directly related to the expected directory location of the file.

    That is, if you have a source file with the package directive of com, it is expected that the file will live in the com directory.

    In your HelloWorld example, it would be expected that the HelloWorld.java file would be stored in the com directory, like com\HelloWorld.java

    When you compile the file, it will create a class file called HelloWorld.class in the com directory, like com\HelloWorld.class

    This way, when Java goes looking for the com.HelloWorld class, it would actually be searching it's class path and appending com\HelloWorld.class to it until it finds your class file or runs out of class path

    Example

    So, I copied your HelloWorld.java (with package) example to C:\java\com\HelloWord.java

    From the command line, I changed to the C:\java directory...

    C:\java>dir com
     Volume in drive C is OS
     Volume Serial Number is ####-####
    
     Directory of C:\java\com
    
    09/08/2013  01:55 PM    <DIR>          .
    09/08/2013  01:55 PM    <DIR>          ..
    09/08/2013  01:55 PM               135 HelloWorld.java
    

    Then I compiled the HelloWorld.java

    C:\java>javac com\HelloWorld.java
    

    Then I ran it...

    C:\java>java com.HelloWorld
    Hello World!
    

    You might like to have a read through Packages tutorial

    0 讨论(0)
  • 2020-12-01 07:58

    The syntax is:

    java -classpath /path/to/package-folder <packageName>.<MainClassName>
    

    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?

    0 讨论(0)
  • 2020-12-01 08:01

    Try to use absolute directory or put your HelloWorld.class into ..\last_directory\com

    1. java -cp .......\last_directory com.HelloWorld
    2. java -cp .......\last_directory HelloWorld(with created com)
    
    0 讨论(0)
  • 2020-12-01 08:01

    You do not need a -cp flag while running a java class , -cp needed while running a class or a main program from a binary (jar) file. While running a main program from a command line you need to make sure you have the class in the same folder structure as package name in the java file, ex.

    home/user/foo java com.test.className   
    
    here classNmae class file and exist under home/user/foo/com/test
    

    hope it helps.

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