jQuery in Action, 2nd edition*
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
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

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 ...
Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

what error do you get? kingly copy paste the error you are getting...
Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

i think you should rename your WEB_INF directory to WEB-INF.
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dev wrote:what error do you get? kingly copy paste the error you are getting...



this is my command screen with compilation errors >>>>

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Jyotirmoy>cd C:\Program Files\Apache Software Foundati
on1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\web

C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\Servle
tsFolder\WEB-INF\classes\com\example\web>javac coffeeselect.java
coffeeselect.java:2: package com.example.model does not exist
import com.example.model.*;
^
coffeeselect.java:19: cannot find symbol
symbol : class CoffeeExpert
location: class com.example.web.CoffeeSelect
CoffeeExpert ce = new CoffeeExpert();
^
coffeeselect.java:19: cannot find symbol
symbol : class CoffeeExpert
location: class com.example.web.CoffeeSelect
CoffeeExpert ce = new CoffeeExpert();
^
3 errors

as i am new to this things so i may be wrong in the compilation process (i guess)... and by the way , thanks Anupam for your quick view to my problem... please rply this to me now what should i do...
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dev wrote:i think you should rename your WEB_INF directory to WEB-INF.



Sorry Anupam, in my first post i made a spelling mistake, it's WEB-INF.. still the problem ocuurs...
Mikael Romo
Greenhorn

Joined: Jun 14, 2011
Posts: 6

Hi,


can you post here the exact code of your servlet?

Tips:

- make sure the specified package is exist.
- and make sure the class that you've instantiate is on the package..
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Welcome to CodeRanch, J Das!

Looks like the compiler cannot find the Java class. Add a classpath entry to contain the path to the Java class "CoffeeExpert". (e.g.: javac -cp <PathToClass & servlet jar> <SourceFiles>). You may want to check our faq entry: HowToSetTheClasspath.


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

Never put your working folders inside the server instance. Use another directory in your home directory for development, then you may build and deploy to the server.


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

Joined: Jul 27, 2011
Posts: 83

i am almost sure this is a matter of the so called 'classpath' issue... in some forums i have seen (and also in the HFSJ book) that during the servlet compilation , the classpath is mentioned in the command prompt ... but the thing is that i don't understand how this things work.. please explain me here in simple words how this things work( i have read some discussions but i couldn't get it ) and please advice me what should i write at the command prompt in order to compile my servlet..??? hoping for quick replies...
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Myk Romo wrote:Hi,


can you post here the exact code of your servlet?

Tips:

- make sure the specified package is exist.
- and make sure the class that you've instantiate is on the package..


Thanks Myk for looking into my matter... below is my servlet code .. it's named CoffeeSelect.java and its address is C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\web




J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Vijitha Kumara wrote:Welcome to CodeRanch, J Das!

Looks like the compiler cannot find the Java class. Add a classpath entry to contain the path to the Java class "CoffeeExpert". (e.g.: javac -cp <PathToClass & servlet jar> <SourceFiles>). You may want to check our faq entry: HowToSetTheClasspath.


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

Never put your working folders inside the server instance. Use another directory in your home directory for development, then you may build and deploy to the server.


Hello Vijitha..i am glad that you offered a helping hand ... 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


Now keeping this structure , what should i be doing.....??? i am totally puzzled..
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Since you already have set the CLASSPATH to servlet-api.jar you can append the new entry while compiling the source.

javac -cp %CLASSPATH%;<PathToTheClass> <SourceFiles>

Replace the "<PathToTheClass>" with the path to your compiled POJO class. It should be to the directory where your package folder exists. (I guess I got it right in the path separator for entries in Windows, if not try ":" instead ";" to separate the entries.)
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Vijitha Kumara wrote:Since you already have set the CLASSPATH to servlet-api.jar you can append the new entry while compiling the source.

javac -cp %CLASSPATH%;<PathToTheClass> <SourceFiles>

Replace the "<PathToTheClass>" with the path to your compiled POJO class. It should be to the directory where your package folder exists. (I guess I got it right in the path separator for entries in Windows, if not try ":" instead ";" to separate the entries.)


When you say <PathToTheClass> , does that mean this>>
C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\model

what i mean to ask you is should that <PathToTheClass> path be the whole actual path?? i mean what i have listed above in bold text ... that one???
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

You can use either absolute path or the relative path. And it should be excluding the package directories.

Assuming you have the compiled POJO class in com/example/model inside the classes directory: you can change to the classes directory and use the dot(.) instead of that path and it should compile (i.e.: %CLASSPATH%;.)
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

By the way when i wrote this at the command prompt....

C:\>javac -cp %CLASSPATH%;C:\Program Files\Apache Software Foundation1\Apache To
mcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\model C:\Program
Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WE
B-INF\classes\com\example\web\CoffeeSelect.java


The following errors showed up in command prompt...

javac: invalid flag: Files\Apache
Usage: javac <options> <source files>
use -help for a list of possible options


What is going on.... i am really confused...
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

You have to escape the spaces in the directory path. Use quotes to wrap the path and see.
Try this to compile if you need it in this way:

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


BTW, you can try what I said before also: cd to classes directory and ....
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Vijitha Kumara wrote:You have to escape the spaces in the directory path. Use quotes to wrap the path and see.
Try this to compile if you need it in this way:

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


BTW, you can try what I said before also: cd to classes directory and ....


I did as you told.. i opened the command prompt and did the following..

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Jyotirmoy>cd\

C:\>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"


But again , these errors showed up....

javac: invalid flag: Files\Apache
Usage: javac <options> <source files>
use -help for a list of possible options


Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

@J Das
It's for sure, you have a classpath problem...
the error says it can't find the package

coffeeselect.java:2: package com.example.model does not exist

so first of all goto the web-inf/classes directory and list the contents (the java file names) and check whether in each java file you have used the proper package statements in each file.....

J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dev wrote:@J Das
It's for sure, you have a classpath problem...
the error says it can't find the package

coffeeselect.java:2: package com.example.model does not exist

so first of all goto the web-inf/classes directory and list the contents (the java file names) and check whether in each java file you have used the proper package statements in each file.....



Please look at the following.. 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

J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dev wrote:@J Das
It's for sure, you have a classpath problem...
the error says it can't find the package

coffeeselect.java:2: package com.example.model does not exist

so first of all goto the web-inf/classes directory and list the contents (the java file names) and check whether in each java file you have used the proper package statements in each file.....



i opened the command prompt and did the following..

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Jyotirmoy>cd\

C:\>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"


But again , these errors showed up....

javac: invalid flag: Files\Apache
Usage: javac <options> <source files>
use -help for a list of possible options
Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

List the contents of CoffeeExpert.java. The problem is in this file i think...
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dev wrote:List the contents of CoffeeExpert.java. The problem is in this file i think...


Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

ha ha got the culprit.....
you have to write

package com.example.web

Does this solve your problem???
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dev wrote:ha ha got the culprit.....
you have to write

package com.example.web

Does this solve your problem???


after compiling i got the following error again....

javac: invalid flag: Files\Apache
Usage: javac <options> <source files>
use -help for a list of possible options


what is this indicating???
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dev wrote:ha ha got the culprit.....
you have to write

package com.example.web

Does this solve your problem???



CoffeeExpert.java is in C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\model

.......... so why should we write

package com.example.web

in CoffeeExpert.java file???
Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

oops!!! sorry for that i thought you have put them both in the same directory... apologies
Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

refer to this page
http://www.coderanch.com/t/453337/java/java/Invalid-Flag-Error-when-javac
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83



i have done whatever there is written in the discussion you reffered to.....
I have enclosed the paths in quotes so as to remove the white spaces as suggested in the discussion.... But still i am getting this error..

javac: invalid flag: Files\Apache
Usage: javac <options> <source files>
use -help for a list of possible options



Please help me i am seriously knocking my head off....
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Bear Bibeault wrote:This has nothing to do with servlets but with learning how to compile from the command line, so it has been moved to a more appropriate location.


You mean everything is fine other than some syntax error at the command prompt ???
Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

Use this

C:\>javac -cp "C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\" "C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\web\CoffeeSelect.java"
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Anupam Dev wrote:Use this

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


I did what you told but same error reported man...
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Try this:

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"


It's probably the trailing slash in -cp entry causing the issue...
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Vijitha Kumara wrote:Try this:

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"


It's probably the trailing slash in -cp entry causing the issue...


I removed the trailing slash in -cp but the same error showed up...

javac: invalid flag: Files\Apache
Usage: javac <options> <source files>
use -help for a list of possible options
Sakthi Sivram
Greenhorn

Joined: Oct 05, 2009
Posts: 1
J Das wrote:
Anupam Dev wrote:what error do you get? kingly copy paste the error you are getting...



this is my command screen with compilation errors >>>>

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Jyotirmoy>cd C:\Program Files\Apache Software Foundati
on1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes\com\example\web

C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\Servle
tsFolder\WEB-INF\classes\com\example\web>javac coffeeselect.java
coffeeselect.java:2: package com.example.model does not exist
import com.example.model.*;
^
coffeeselect.java:19: cannot find symbol
symbol : class CoffeeExpert
location: class com.example.web.CoffeeSelect
CoffeeExpert ce = new CoffeeExpert();
^
coffeeselect.java:19: cannot find symbol
symbol : class CoffeeExpert
location: class com.example.web.CoffeeSelect
CoffeeExpert ce = new CoffeeExpert();
^
3 errors

as i am new to this things so i may be wrong in the compilation process (i guess)... and by the way , thanks Anupam for your quick view to my problem... please rply this to me now what should i do...



Try this one,

From command prompt go to the path,
C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes>

Then set classpath like below,
set CLASSPATH=%CLASSPATH%;C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\Servle
tsFolder\WEB-INF\classes;

Then compile your servletcode like,
javac com/example/web/CoffeeSelect.java

The problem is "CoffeeSelect" cannot able to find the class file "CoffeeExpert" since you are compiling from "CoffeeSelect" path. Refer your code above.


Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Welcome to CodeRanch, Sakthi Sivram!

The latest problem the OP facing is that the command line does not interpret the arguments to javac properly.

@J Das
At this point you can try two things.

1. Add the entry for the compiled POJO class in to the CLASSPATH environment
variable, and try compiling.
2. Go to the .../WEB-INF/classes directory and try compiling as I said before.

(Note: You can avoid most of these problems by using a seperate path for your development work.)
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Sakthi Sivram wrote:

Try this one,

From command prompt go to the path,
C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes>

Then set classpath like below,
set CLASSPATH=%CLASSPATH%;C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\Servle
tsFolder\WEB-INF\classes;

Then compile your servletcode like,
javac com/example/web/CoffeeSelect.java

The problem is "CoffeeSelect" cannot able to find the class file "CoffeeExpert" since you are compiling from "CoffeeSelect" path. Refer your code above.




I tried the following in my command prompt..(but in vain ) I know i have done something really wrong ... Point me where i did went wrong..??

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Jyotirmoy>cd C:\Program Files\Apache Software Foundati
on1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes

C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\Servle
tsFolder\WEB-INF\classes>set CLASSPATH=%CLASSPATH%;C:\Program Files\Apache Softw
are Foundation1\Apache Tomcat 6.0.26\webapps\ServletsFolder\WEB-INF\classes;java
c com/example/web/CoffeeSelect.java

C:\Program Files\Apache Software Foundation1\Apache Tomcat 6.0.26\webapps\Servle
tsFolder\WEB-INF\classes>
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Vijitha Kumara wrote:Welcome to CodeRanch, Sakthi Sivram!

The latest problem the OP facing is that the command line does not interpret the arguments to javac properly.

@J Das
At this point you can try two things.

1. Add the entry for the compiled POJO class in to the CLASSPATH environment
variable, and try compiling.
2. Go to the .../WEB-INF/classes directory and try compiling as I said before.

(Note: You can avoid most of these problems by using a seperate path for your development work.)


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


Then after changing the CLASSPATH i again tried doing this at the command prompt...

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"


Still getting that same error

javac: invalid flag: Files\Apache
Usage: javac <options> <source files>
use -help for a list of possible options


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 seperate path for your development work.)



Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

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?

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 did the following >>>>>>>
made a directory in my D:/new folder
and build the directory tree as follows
D:\new folder\com\web
D:\new folder\com\model

and then i created two classes as told by you
D:\new folder\com\web\abc.java
D:\new folder\com\model\xyz.java




i compiled abc,java using
D:\new folder\com\web>javac abc.java

it was successfully compiled..
But then......



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

it did not compile... gave the following error...

D:\new folder\com\model\xyz.java:7: <identifier> expected
a.add();
........^
1 error


What is going on ??? please explain me..
Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

No that's wrong

a.add() in xyz.java must be in public static void main(Sting args[]). you can't call a method like this from outside...

add method in abc.java must be public
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Error in Servlet compilation