Run from command line, Wrong Name error

后端 未结 6 1897
栀梦
栀梦 2020-12-22 07:58

I want to run a Java project from the command line which I start using a batch file, but I get the wrong name error.

The directory setup:

  • srcMVC
      <
6条回答
  •  情书的邮戳
    2020-12-22 08:30

    java bin/main.Main is wrong, you must specify -cp here:

    java main.Main -cp bin
    

    Here the first argument is the class name which should be found in the classpaths, rather than the class file location. And -cp just adds the logical path to classpaths. You should make the root of your project searchable in the classpath.

    and for those javac commands, you have already specified the correct path, so you don't need -cp src. The difference here is the javac command uses logical path for .java files, while using java command you could only specify the path in -cp attribute.

    You could also execute java main.Main without -cp if you enter the directory bin:

    cd bin
    java main.Main
    

    Since the current path will be automatically be searched by java as a classpath.

提交回复
热议问题