aspose file tools*
The moose likes Beginning Java and the fly likes Error in Servlet compilation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error in Servlet compilation " Watch "Error in Servlet compilation " New topic
Author

Error in Servlet compilation

J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dee wrote:Try this first

make a directory in your d:/new folder (do not remove the space between new and folder)
and build the directory tree as follows
d:\new folder\com\web
d:\new folder\com\model

and then create two classes
d:\new folder\com\web\abc.java
d:\new folder\com\model\xyz.java

abc.java
----------
package com.web;
class abc
{

}


xyz.java
----------
package com.model;
import com.web.*;
class xyz
{
abc a=new abc();
}


and then compile using
c:\> javac -cp "d:\new folder" "d:\new folder\com\model\xyz.java"

does this compile on your system successfully?



Again when i exactly followed your codes and proceeded it gave me the following error...
C:\>javac -cp "D:\new folder "D:\new folder\com\model\xyz.java"
javac: file not found: folder\com\model\xyz.java
Usage: javac <options> <source files>
use -help for a list of possible options

Any clue ??? i am totally confused..
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dee wrote:Try this first

make a directory in your d:/new folder (do not remove the space between new and folder)
and build the directory tree as follows
d:\new folder\com\web
d:\new folder\com\model

and then create two classes
d:\new folder\com\web\abc.java
d:\new folder\com\model\xyz.java

abc.java
----------
package com.web;
class abc
{
}


xyz.java
----------
package com.model;
import com.web.*;
class xyz
{
abc a=new abc();
}


and then compile using
c:\> javac -cp "d:\new folder" "d:\new folder\com\model\xyz.java"

does this compile on your system successfully?



Again when i exactly followed your codes and proceeded it gave me the following error...
C:\>javac -cp "D:\new folder "D:\new folder\com\model\xyz.java"
javac: file not found: folder\com\model\xyz.java
Usage: javac <options> <source files>
use -help for a list of possible options


Any clue ??? i am totally confused..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38879
    
  23
The add call must be inside a method (or initialiser), not necessarily the main method.
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dee wrote:Try this first

make a directory in your d:/new folder (do not remove the space between new and folder)
and build the directory tree as follows
d:\new folder\com\web
d:\new folder\com\model

and then create two classes
d:\new folder\com\web\abc.java
d:\new folder\com\model\xyz.java

abc.java
----------
package com.web;
class abc
{
}


xyz.java
----------
package com.model;
import com.web.*;
class xyz
{
abc a=new abc();
}


and then compile using
c:\> javac -cp "d:\new folder" "d:\new folder\com\model\xyz.java"

does this compile on your system successfully?



I made the following changes and both abc.java and xyz.java got compiled....

abc.java


i compiled it in the command prompt as---

D:\new folder\com\web>javac abc.java



xyz.java



i compiled it in the command prompt as---

C:\>javac -cp "D:\new folder" "D:\new folder\com\model\xyz.java"


Now it has compiled .. but i have a query... why did i have to compile the xyz.java from C:\ ??? why didn't we compile it from D:\ like in the case of abc.java.. I know these are very basic things but i would like to know this through this discussion because i am starting to love learning through this discussion... So kindly help me out...
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
J Das wrote:hello everyone... i have just joined JAVARANCH today.. so in posting my queries if you find some syntactic errors please excuse me.... recently i have been following the Head First Servlets and JSP book for beginning with some web app.. the problem i am facing is in compiling the second version of the servlet program thats been illustrated in chapter 3.... I am using Windows Xp , i have Tomcat version 6 installed on my system and also i have JDK 1.6.x installed... What i have done by far is this---->>>>

* The folder where my project is ServletsFolder whose path is---
C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder

*Inside the folder (ServletsFolder) i have a WEB_INF folder and a html form named form.html

*The WEB_INF contains a deployment descriptor (web.xml) file and a folder named (classes).

*the classes folder contains (com\example\web) and (com\example\model) where the web folder contains the servlet (CoffeeSelect.java) and the model folder contains the plain java class(CoffeeExpert.java) whom the servlet invokes.


the situation so far.... when i compile the plain java (CoffeeExpert.java), it gets compiled but the servlet doesn't compile ... may be i am wrong in my compiling procedure because i am new to servlets and things like Classpath, setting Environment variables etc.. so can any one let me know the way this things should be done..??? please help me out i am stuck with this badly.... repi need this to be solved as soon as possible .. hoping to get quick replies ...


If you are using tomcat add servlet-api.jar to the classpath before compiling the servlet java file.
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Campbell Ritchie wrote:The add call must be inside a method (or initialiser), not necessarily the main method.


Thanks for your suggestion Campbell R.

By the way , my main problem is that i am not been able to compile my servlet .. Kindly take a look at my posts where i have illustrated the various problems that i am facing... Thanks again and hoping for a solution from you soon...
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Ashutosh Limaye wrote:
J Das wrote:hello everyone... i have just joined JAVARANCH today.. so in posting my queries if you find some syntactic errors please excuse me.... recently i have been following the Head First Servlets and JSP book for beginning with some web app.. the problem i am facing is in compiling the second version of the servlet program thats been illustrated in chapter 3.... I am using Windows Xp , i have Tomcat version 6 installed on my system and also i have JDK 1.6.x installed... What i have done by far is this---->>>>

* The folder where my project is ServletsFolder whose path is---
C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder

*Inside the folder (ServletsFolder) i have a WEB_INF folder and a html form named form.html

*The WEB_INF contains a deployment descriptor (web.xml) file and a folder named (classes).

*the classes folder contains (com\example\web) and (com\example\model) where the web folder contains the servlet (CoffeeSelect.java) and the model folder contains the plain java class(CoffeeExpert.java) whom the servlet invokes.


the situation so far.... when i compile the plain java (CoffeeExpert.java), it gets compiled but the servlet doesn't compile ... may be i am wrong in my compiling procedure because i am new to servlets and things like Classpath, setting Environment variables etc.. so can any one let me know the way this things should be done..??? please help me out i am stuck with this badly.... repi need this to be solved as soon as possible .. hoping to get quick replies ...


If you are using tomcat add servlet-api.jar to the classpath before compiling the servlet java file.


i have my classpath settings done as :
Variable name:CLASSPATH
Variable value:C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\lib\servlet-api.jar

for JAVA_HOME...
Variable name:JAVA_HOME
Variable value:C:\Program Files\Java\jdk1.6.0_07

for TOMCAT_HOME...
Variable name:TOMCAT_HOME
Variable value:C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26

and Path...
Variable name:Path
Variable value:C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Program Files\Common Files\Teleca Shared;C:\Program Files\Java\jdk1.6.0_07\bin

But still cannot compile...
Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

i just can't understand...
i created the same scenario on my system...but i m able to compile it successfully....i have simply copy and pasted your code... and your javac commands and all that...
and it got compiled in one go...
i am not able to understand why is it so...
create two classes in the same web-inf\classes\com\web and model directories... let the package and import statements be as such... but create empty classes....no methods no variables nothing...even don't extend HttpServlet and then try to compile... still if you are getting errors then...google is the best option i think...
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dee wrote:i just can't understand...
i created the same scenario on my system...but i m able to compile it successfully....i have simply copy and pasted your code... and your javac commands and all that...
and it got compiled in one go...
i am not able to understand why is it so...
create two classes in the same web-inf\classes\com\web and model directories... let the package and import statements be as such... but create empty classes....no methods no variables nothing...even don't extend HttpServlet and then try to compile... still if you are getting errors then...google is the best option i think...


Good news... it compiled... I mean the one you told me to do...
I did the following as suggested by you...

I created a file named FileModel.java in C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\model


the code for FileModel.java is the following...


then I created a file named FileWeb.java in C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\web

the code for FileWeb.java is the following...


I was successful in compiling both these files... Now tell me where i am going wrong with my original problem... Please re-check the whole post (...if possible ) and suggest me what's going wrong???
By the way thanks for your attachment with my problem.. have learned many things from you guys... hope to get a solution in quick time..
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

J Das wrote:I changed the CLASSPATH in my environment variable to the following...

C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\lib\servlet-api.jar;C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\model

That's wrong. It should not contain package directories as I said before. Please check my previous reply.
and by the way what did you mean when you said
Vijitha Kumara wrote:
(Note: You can avoid most of these problems by using a separate path for your development work.)

That's you can avoid these lengthy path settings which are causing these errors.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

It finally compiled.... THANKS A TON to everyone who participated in my matter.... I am very happy now and relieved as well...

I compiled it this way...

C:\>javac -cp "C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26
\webapps\ServletsFolder\WEB-INF\classes" "C:\Program Files\Apache Software Found
ation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\w
eb\CoffeeSelect.java"


Before I was compiling in the following way...

javac -cp %CLASSPATH%;"C:\Program Files\Apache Software Foundation1\Apache T
omcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes" "C:\Program Files\Apache S
oftware Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\
com\example\web\CoffeeSelect.java"


Now i am going to ask all of you a very dummy question (in my opinion).... Please clear my doubts here...
In both the versions ( the one which compiled and the one which didn't ) , we see only a difference in writing the term " %CLASSPATH%; " .... So I wanted to know exactly what is "-cp" and what was that "%CLASSPATH%;" thing all about.... Please Please Please make me understand these things.. Although it got compiled i still could'nt make out the reasons why...


Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

Glad you solved it
. So I wanted to know exactly what is "-cp" and what was that "%CLASSPATH%;" thing all about.

Using -cp switch would override any entries you have set in CLASSPATH environment variable. Including it along with -cp switch would append new entries to it so that javac/java etc... will pick them all.
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Using -cp switch would override any entries you have set in CLASSPATH environment variable. Including it along with -cp switch would append new entries to it so that javac/java etc... will pick them all.


Could you briefly explain it to me... ??? Using my example can you explain it to me??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38879
    
  23
If you use
-cp c:\foo

means your entire classpath is the foo directory. You will even lose the . for current directory from the classpath.

If you use
-cp %CLASSPATH%;c:\foo

you are adding the foo directory to the end of the old classpath. The foo directory is now in addition to the old classpath.
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Campbell Ritchie wrote:If you use
-cp c:\foo

means your entire classpath is the foo directory. You will even lose the . for current directory from the classpath.

If you use
-cp %CLASSPATH%;c:\foo

you are adding the foo directory to the end of the old classpath. The foo directory is now in addition to the old classpath.


Great... Now i understand.. Thanks for clearing me out.. Cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in Servlet compilation