我为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
这就是我在做什么:


运行java -jar swagger-codegen-cli-2.1.6.jar meta \ -o output/myLibrary -n myClientCodegen -p com.my.company.codegen创建服装代码生成
在output / myLibrary中运行mvn package
在包含swagger-codege-cli-2.1.6.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 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


我已经看过“错误:找不到或加载主类”问题的答案,但是还没有解决。

Here is a link to the jar

最佳答案

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

如果您在根项目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构造类路径时,您对swagger-codegen-cli-2.1.6.jar会有错误。请测试以下内容。

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

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


javap检查主类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[]);
}

09-04 03:36