Error: Could not find or load main class Swagger Codegen

Error: Could not find or load main class Swagger Codegen

我为 here

留下了未解决的问题

我正在尝试创建一个自定义代码生成器,我设法通过将文件放入代码生成器项目中使其工作,但我希望它像这样工作:https://github.com/swagger-api/swagger-codegen#making-your-own-codegen-modules

我根本没有修改自动生成的项目,但我不断收到:

Error: Could not find or load main class io.swagger.codegen.SwaggerCodegen

这是命令行:

java -cp output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar:swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation 

我从这里得到罐子 https://mvnrepository.com/artifact/io.swagger/swagger-codegen-project/2.1.6 这就是我正在做的:

  1. 运行 java -jar swagger-codegen-cli-2.1.6.jar meta \ -o output/myLibrary -n myClientCodegen -p com.my.company.codegen 创建服装代码生成器

  2. 运行 mvn package 在 output/myLibrary

  3. 运行 java -cp output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar:swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation 在包含 swagger-codege-cli-2.1.6.jar 和输出文件夹的文件夹中

如果我删除第一部分,它确实找到了 class,但没有找到新语言:

java -cp swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation 

我查看了 "Error: Could not find or load main class" 个问题的答案,但未能解决。

Here is a link to the jar

问题是您没有在调用中指定 swagger-codegen-2.1.6.jar 的正确路径。这就是为什么它找不到 main-class.

如果你在根项目中 swagger-codegen 你应该这样指定它:modules/swagger-codegen-cli/target/swagger-codegen-cli.jar

~$ cd ~/git/swagger-codegen # go into your root project
~/git/swagger-codegen$ # ... do the steps you described
~/git/swagger-codegen$ java  -cp \
 output/myLibrary/target/myClientCodegen-swagger-codegen-1.0.0.jar:modules/swagger-codegen-cli/target/swagger-codegen-cli.jar \
 io.swagger.codegen.SwaggerCodegen \
 generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json \
 -l com.my.company.codegen.Mycustomcodegengenerator \
 -o outputlocation

或单线:

~/git/swagger-codegen$ java  -cp output/myLibrary/target/myClientCodegen-swagger-codegen-1.0.0.jar:modules/swagger-codegen-cli/target/swagger-codegen-cli.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation

更新 1

我很确定当您使用 -cp 构建 class 路径时,您对 swagger-codegen-cli-2.1.6.jar 有错误。请测试以下内容。

将(myClientCodegen-swagger-codegen-1.0.0.jarswagger-codegen-cli-2.1.6.jar)jar 复制到同一个文件夹中。然后进入此文件夹并尝试以下操作:

javap -cp myCustomCodegen-swagger-codegen-1.0.0.jar:swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen

javap 检查主 class io.swagger.codegen.SwaggerCodegen 是否可用。在我的机器上它打印了这个:

Compiled from "SwaggerCodegen.java"
public class io.swagger.codegen.SwaggerCodegen {
  public io.swagger.codegen.SwaggerCodegen();
  public static void main(java.lang.String[]);
}

对于 Windows,将冒号 (:) 更改为分号 (;) - 在 class 路径中的 jar 之间。所以而不是

java -cp output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar:swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation

应该是

java -cp output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar;swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation

多个class路径需要用分号分隔。 http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

当 运行 作为 Spring 在 macOS 10.14 中的 Eclipse 4.19 中启动应用程序时,我遇到了类似的问题:

"Error: Could not find or load main class io.swagger.Swagger2SpringBoot"

…即使我正盯着这个包含 main 方法的class。我的解决方案是发布一个“maven 更新项目”。与其他具有奇怪 Java 症状的情况一样,此问题已解决。