2

I installed Swagger CodeGen using the command

npm install swagger-codegen -g

So I should have it installed, but there is some error/glitch going on. Everytime I enter

swagger-codegen -v

I get the following message

'swagger-codegen' is not recognized as an internal or external command, operable program or batch file.

I have no idea what to do. How should I fix this? I am trying to follow a tutorial, but the instructor nevers shows how he installs programs.

Under GitHub, the instructions don't make sense

Prerequisites If you're looking for the latest stable version, you can grab it directly from Maven.org (Java 7 runtime at a minimum):

wget http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar -O swagger-codegen-cli.jar

java -jar swagger-codegen-cli.jar help

For Windows users, you will need to install wget or you can use Invoke-WebRequest in PowerShell (3.0+), e.g.

Invoke-WebRequest -OutFile swagger-codegen-cli.jar http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar

Am I supposed to put those in the command line? tried that, didn't work

bockdavidson
  • 2,083
  • 3
  • 14
  • 21

2 Answers2

12

Swagger Codegen is a Java app, so you must have Java installed.

There are two versions of Swagger Codegen - 2.x and 3.x. Use 3.x for openapi: 3.0.0 definitions and 2.x for swagger: '2.0'.

Installing Swagger Codegen CLI (Command-Line Version)

Windows

Download the compiled JAR file:

Look for the file named swagger-codegen-cli-<version>.jar, e.g. if you need v. 2.3.1:
http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar

Swagger Codegen JAR file


To run the codegen, use:

java -jar c:\path\to\swagger-codegen-cli-<version>.jar <args>

(You can rename swagger-codegen-cli-<version>.jar to swagger-codegen-cli.jar for simplicity.)

Mac

Install using Homebrew:

# Codegen 3.x
brew install swagger-codegen

# Codegen 2.x
brew install swagger-codegen@2

Then you can run the codegen as:

swagger-codegen <args>
swagger-codegen@2 <args>

If you don't have Homebrew, download the JAR file from Maven and run it as explained above for Windows.

Alternatives to CLI Version

You can also:

Helen
  • 87,344
  • 17
  • 243
  • 314
  • I am using a Windows. I downloaded the jar file, renamed it to swagger-codegen-cli.jar on my command line,, just simply enter java -jar swagger-codegen-cli.jar? When I do that I get the message 'java' is not recognized as an internal or external command, operable program or batch file. – bockdavidson Aug 07 '18 at 14:28
  • now when I type the command java -jar swagger-codegen-cli.jar I get Error: Unable to access jarfile swagger-codegen-cli.jar – bockdavidson Aug 07 '18 at 14:41
  • Swagger Codegen is a Java app, so you need Java. Make sure to specify the path to `swagger-codegen-cli.jar` in your command. – Helen Aug 07 '18 at 14:50
  • ok, still new to this stuff. I know I go to environment variables, go to path add it there. Do I just add swagger-codegen-cli.jar to the path or does it have to be in a special format? entering java -jar C:\swagger-codegen-cli.jar doesn't work either, which is where my jar file is – bockdavidson Aug 07 '18 at 14:52
  • You don't need to add JAR file paths to the `PATH`, just use the full path in the `java -jar path\to\jarfile` command. *"Doesn't work either"* - how exactly does it not work? What is the error message? – Helen Aug 07 '18 at 15:03
  • my jar file is under my C: section, so the only path I have to specify is just C:\... – bockdavidson Aug 07 '18 at 15:06
  • I tried that, doesn't work, even if I specify the path so when I enter `java -jar C:\swagger-codegen-cli.jar` I still get the message Error: Unable to access jarfile C:\swagger-codegen-cli.jar – bockdavidson Aug 07 '18 at 15:13
  • Try moving the JAR to a subfolder instead, e.g. `C:\codegen\`. – Helen Aug 07 '18 at 15:22
  • I created another folder called codegen and put my jar file in it so the command should be `java -jar C:\codegen\swagger-codegen-cli.jar`, still cannot access it – bockdavidson Aug 07 '18 at 15:25
  • I decided to delete and redownload it, now when I type the command in I guess it works, it prints a large message that looks right, first part saying available languages, in the tutorial, the instructor entered `swagger-codegen --version` , I did that but still get 'swagger-codegen' is not recognized as an internal or external command, operable program or batch file. – bockdavidson Aug 07 '18 at 15:43
  • 1
    ok, I got it working, the instructor is using a mac so commands are a bit different. had to enter `java -jar C:\swagger-codegen-cli-2.3.1.jar --version` thank you so much! – bockdavidson Aug 07 '18 at 15:47
-1

this module doesn't create an executable file when being installed via npm install swagger-codegen -g, that's why you see the error. Please see https://github.com/swagger-api/swagger-codegen/blob/master/README.md#prerequisites for installation instructions

lena
  • 90,154
  • 11
  • 145
  • 150
  • I tried looking at that, but it is very confusing. I downloaded the entire zip file, not sure what to do with that. Under installation, I tried using those commands in the command line, but never worked – bockdavidson Aug 06 '18 at 19:03