File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes classpath issue while compiling through cmd prompt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

classpath issue while compiling through cmd prompt

John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Hi,

I'm new to servlets and I was working on my first servlet program. (using Head First Servlets & JSP book).

When i try to compile it through command prompt I had to give paths to my classfiles. (UserSelect.java references other files from classes\model\...)
But I had to include the path to the jar file also since " -cp " overrides the CLASSPATH set in environment variable (in Win).


javac -d C:\Jspace\Ecafe\classes -cp "C:\Jspace\Ecafe\classes";"C:\From Pen\Tomcat\apache-tomcat-5.5.26\apache-tomcat-5.5.26\common\lib\servlet-api.jar" web\UserSelect.java

Is there a better way to do this? I tried to include "C:\Jspace\Ecafe\classes" in the CLASSPATH environment variable as below

%CLASSPATH%;C:\Jspace\Ecafe\classes;C:\From Pen\Tomcat\apache-tomcat-5.5.26\apache-tomcat-5.5.26\common\lib\servlet-api.jar;

but it doesn't seem to work. Also I would like to know what %CLASSPATH% means?


www.cs-repository.info
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Please take a look at this.

Thanks,
Srikkanth
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14192
    
  20

On Windows, you must put directory names that contain spaces between quotes:

set CLASSPATH=%CLASSPATH%;C:\Jspace\Ecafe\classes;"C:\From Pen\Tomcat\apache-tomcat-5.5.26\apache-tomcat-5.5.26\common\lib\servlet-api.jar";.

Note that you did this with the "-cp" switch - you must also do it when you set the CLASSPATH environment variable.

%CLASSPATH% is the value of the CLASSPATH variable, so the line above says "Set CLASSPATH to whatever is in the CLASSPATH now plus this additional stuff".

Note the "." as the last entry of the CLASSPATH - "." means "the current directory".

When you're writing larger programs and you want to be able to compile them from the command line easily, it's better to learn and use a build tool such as Ant.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
ok... thanks all. That was quick reply. The problem was with the directory name as rightly pointed out Jesper.
It compiles fine now. :-)
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Jesper,

"." is not mandatory right? I compile my application from C:\JSpace\Ecafe\src\com\example> and my class files are in C:\JSpace\Ecafe\classes so there is no need to add "." i believe.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Moving to servlets forum. I would recommend you keep the . in your classpath, in case you invoke a file in your current directory.
Only set a classpath with the -cp flag, or (for a lasting change) with the set instruction on the command prompt. Don't set it in "environment variables" unless you can't find files in the current directory.
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Hello everyone,

It was working fine till yesterday but again today while i compile i get " javax.servlet.http not found" error.

Where do i physically find this file. I thought it is available with the src collection inside JDK folder. But I couldn't find it there.

John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
I found it within the jar file bundled with Tomcat. Wonder what could be the mistake in classpath as I had given exactly as above and it worked yesterday.
Soumil Shah
Ranch Hand

Joined: Jul 13, 2009
Posts: 54
Have you included classpath in Window's Environment variable or only at command prompt while compiling?
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
at command line using the cmd set CLASSPATH.
Dhanashri Tilekar
Greenhorn

Joined: Feb 06, 2014
Posts: 11
Hi..

I am also getting the same issue..did you get a fix for this? If yes can you please share it here..

Regards,
Disha
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Yes it got fixed. Are you still facing any issues.
Dhanashri Tilekar
Greenhorn

Joined: Feb 06, 2014
Posts: 11
Hello,

Thanks for your reply

I am getting the following error..

C:\Program Files\Apache-Tomcat-7\apache-tomcat-7.0.53\webapps\DEMO\WEB-INF\class
es>javac DemoServlet.java
javac: file not found: DemoServlet.java
Usage: javac <options> <source files>
use -help for a list of possible options

I have set the CLASSPATH VARIABLE, PATH VARIABLE, JAVA_HOME VARIABLE....All these Environmental variables I have set in system settings-Advanced settings.

I am not getting what is going wrong here..I am new to servlets

Thanks and Regards,
Disha
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42028
    
  64
Please do not post the same question multiple times. Let's continue the discussion in this duplicate thread.


Ping & DNS - my free Android networking tools app
 
 
subject: classpath issue while compiling through cmd prompt