Dear All,

I am trying to learn j2ee from the basics. I have written a basic program from my guidance book and when i try to compile it , i get the below error.
Please throw some light on it.

javac -classpath C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/lib/servlet-api.jar:classes:. -d classes src/com/example/web/BeerSelect.java
javac: invalid flag: (x86)/Apache
Usage: javac <options> <source files>
use -help for a list of possible options

Dani AI

Generated

Nice catch by — the error comes from the unquoted classpath with a space in "Program Files (x86)". When the shell hands arguments to javac without quotes, the path is split on spaces and parts like (x86)/Apache get treated as separate tokens, which javac then flags as invalid. confirmed that quoting fixed the immediate problem.

A couple of important clarifications and best practices:

  • On Windows the classpath separator is ; (semicolon). On Unix-like systems it is : (colon). Quote the entire -classpath value so the shell keeps it as one argument. Example (Windows):
javac -classpath "C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar;classes;." -d classes src\com\example\web\BeerSelect.java

Example (Unix/Linux/macOS):

javac -classpath "/path/to/tomcat/lib/servlet-api.jar:classes:." -d classes src/com/example/web/BeerSelect.java
  • PowerShell has different quoting rules (single quotes are safer for literal paths). If using Git Bash or Cygwin on Windows, the colon-style separator may work there; on cmd.exe use ;.

Additional tips: confirm the JAR actually exists at the given path, and ensure the classes output directory is present (or created). For real projects, use a build tool (Maven/Gradle) and mark the servlet API as "provided" so it is used at compile time but not bundled into the WAR — bundling the container's servlet JAR causes classloading conflicts at runtime.

This addresses the immediate syntax problem (spaces + quoting) and also points toward safer, long-term build practices.

Recommended Answers

All 2 Replies

I suspect your problem has to do with whitespaces. Try enclosing the path in quotes("):

javac -classpath "C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/lib/servlet-api.jar":classes:. -d classes src/com/example/web/BeerSelect.java

Hi tinstaafl,

Thanks for the advise, its working now !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.