aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Command line arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Command line arguments" Watch "Command line arguments" New topic
Author

Command line arguments

Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Hi all

I have a problem regarding command line arguments from the Head First JSP.

I am referencing pg 81 Compiling the servlet

%cd MyProjects/beerV1

%javac -classpath /Users/bert/Applications2/comman/lib/servlet-api.jar
:classes:. -d classes src/com/exampl/web/BeerSelect.java

I fail to understand the the command highlighted in bold above ":classes:.".

When I tried to compile using the above command I got errors such as'cannot find HttpServlet'.

Therefore I removed the ':classes:.' ,and than it compiled fine.

My question is that I dont understand what the ':classes' command does and what effect it will have on the compilation of my code.
Tobin Jackson
Greenhorn

Joined: Sep 01, 2004
Posts: 17
Abdulla,

I'm stumped on why removing ":classes:." would cause that type of error. If you're thinking of ":classes:." as a command/flag tho that would be wrong. It's just more path info for the -classpath flag. Make sure there are no spaces between ":classes:." and the path info preceding it ".../servlet-api.jar". I say that because if you're thinking of it as a command you might have put a space there

Tobin
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Hi

Okay, I got the point that :classes:. is not a command.

Is :classes:. a subfile in the servlet-api jar ?
How does the '.' operator function in this case ?

I am still getting the same error with :classes:.
Also when I try to compile the code on pg84, I get an error stating that the compiler cannot find the package 'com.example.model'
Bryan Basham
author
Ranch Hand

Joined: Apr 30, 2001
Posts: 199
Hello Abdulla,

The javac command takes source files and compiles them into .class files, as you probably know. The basic form of the command is:


The -classpath option provides one or more JAR files or directories that contain .class files that the source file depends upon. Therefore, the command:

tells the compiler to use the servlet API JAR file and the classes directory (a subdirectory of the directory from which you invoked the javac command) while compiling the source file. The : (colon) character is used to separate multiple JAR files or directories. Also note that the . (dot) character tells the compiler to look for classes in the current directory.

The -d option tells the compiler where to store the new .class files generated by compiling the source file.

Does that help?

-Bryan
[ September 13, 2004: Message edited by: Bryan Basham ]
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Thank you, Bryan for giving such a detailed explanation.
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Hi

When I try to compile CoffeeSelect from pg 85 of HF JSP,I am getting the following error messages.

--------------------------------------------------------------------------
C:\MyProject\coffeeV1>javac -classpath /tomcat/common/lib/servlet-api.jar:classes:. -d classes src/com/example/web/CoffeeSelect.java

src/com/example/web/CoffeeSelect.java:3: package com.example.model does not exist
import com.example.model.*;
^
src/com/example/web/CoffeeSelect.java:4: package javax.servlet does not exist
import javax.servlet.*;
^
src/com/example/web/CoffeeSelect.java:5: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
src/com/example/web/CoffeeSelect.java:9: cannot resolve symbol
symbol : class HttpServlet
location: class com.example.web.CoffeeSelect
public class CoffeeSelect extends HttpServlet {
^
src/com/example/web/CoffeeSelect.java:11: cannot resolve symbol
symbol : class HttpServletRequest
location: class com.example.web.CoffeeSelect
public void doPost(HttpServletRequest request, HttpServletResponse respo
nse)
^
src/com/example/web/CoffeeSelect.java:11: cannot resolve symbol
symbol : class HttpServletResponse
location: class com.example.web.CoffeeSelect
public void doPost(HttpServletRequest request, HttpServletResponse respo
nse)
^
src/com/example/web/CoffeeSelect.java:12: cannot resolve symbol
symbol : class ServletException
location: class com.example.web.CoffeeSelect
throws IOException, ServletException {
^
src/com/example/web/CoffeeSelect.java:20: cannot resolve symbol
symbol : class CoffeeExpert
location: class com.example.web.CoffeeSelect
CoffeeExpert be=new CoffeeExpert();
^
src/com/example/web/CoffeeSelect.java:20: cannot resolve symbol
symbol : class CoffeeExpert
location: class com.example.web.CoffeeSelect
CoffeeExpert be=new CoffeeExpert();
^
9 errors

------------------------------------------------------------------------

Please, closely take note of the two errors that are highlighted.

Firstly, it cannot find my model package in which my CoffeeExpert sits.
Secondly, it cannot locate my javax.servlet.

I have installed Tomcat 5.xxx it has the servlet-api jar file. The model package has been setup and coded according to the book. There is no problem with the code I have cross checked several times.

I fail to understand the cause of the above two problems.
Amy Humphries
Greenhorn

Joined: Apr 27, 2004
Posts: 9
I was able to fix the "package javax.servlet does not exist" error by adding j2ee.jar to my classpath. I am also getting "package com.example.model does not exist" and I'm stumped.

This is what I am doing:
C:\dev\MyProjects\beerV1> javac -classpath c:\dev\tomcat\common\lib\servlet-api.jar:classes:.;c:\j2sdkee1.3.1\lib\j2ee.jar -d classes src/com/example/web/BeerSelect.java

There must be something else I have to add to the classpath, but I don't know what.
Amy Humphries
Greenhorn

Joined: Apr 27, 2004
Posts: 9
This worked for me:

javac -classpath c:\dev\tomcat\common\lib\servlet-api.jar:classes:.;c:\j2sdkee1.3.1\lib\j2ee.jar;c:\dev\MyProjects\beerV1\classes -d classes src/com/example/web/BeerSelect.java
Jack Bercy
Greenhorn

Joined: Jul 31, 2004
Posts: 11
Originally posted by Amy Humphries:
This worked for me:

javac -classpath c:\dev\tomcat\common\lib\servlet-api.jar:classes:.;c:\j2sdkee1.3.1\lib\j2ee.jar;c:\dev\MyProjects\beerV1\classes -d classes src/com/example/web/BeerSelect.java



To make things even clearer for Java-newbies that have never installed the SDK and/or have never compiled Java from the command-line (like me !), it seems that the windows version of the SDK requires SEMICOLONS instead of colons to separate the class files in the -classpath argument.

The book says to use

-classpath .../servlet-api.jar:classes:.

On Windows (XP ?), using that will result in "package javax.servlet does not exist" and other errors.

Using instead :

-classpath .../servlet-api.jar;classes;.

will work just fine (for me anyway).

Thus no need for the j2ee sdk classes, the point is that all needed classes are in the tomcat/common/lib directory.

(this must have been posted a zillion times across all forums)...

Does that qualify as a book errata ?
Patrik Nilsson
Ranch Hand

Joined: Oct 05, 2004
Posts: 49
I managed to compile the Java file now but I don't understand why it is working now.

Someone tried to explain in detail what the commandline meant but I'm still confused.
This is what i typed in:

E:\Java\Servlets_JSP\Beer-v>javac -classpath d:/jakarta/common/lib/servlet-
api.jar;classes;. -d classes src/com/example/web/beerSelect.java

Why do I have to set the classpath , isn't that what I did as an environment variable???
I have CLASSPATH = D:\jakarta\common\lib\servlet-api.jar
Do I override the classpath and add the classes or what is really going on here?

what is in this 'classes' and why is it needed? servlet-
api.jar;classes;.

I could compile everything before, following the examples in the book, but when I needed to import the package com.example.model it didn't work anymore.

Anyone knows...???


Sun Certified Java Programmer<br />Sun Certified Web Component Developer<br />IBM Certified Enterprise Developer
Judy Collins
Greenhorn

Joined: Oct 07, 2004
Posts: 20
Guys
Simplest solution to your problems can be add servlet.jar (or servlet-api.jar) and anyother required packages to your classpath.

Another reason of your problems I can see is that there is a differnce in providing classpath on command line in winodws and unix(unix like) systems. command provided in book seems to be good for unix (and its clones). Consult your respective os on how to provide classpath on command lines. Hope this helps.

Regards
Roger Yates
Ranch Hand

Joined: Aug 31, 2004
Posts: 118
Patrik,

To answer your question about ;classes;

classes is the name of the directory that your compiled classes code has been put, based on the "-d classes" flag you are using for the javac command line. ("-d" says "put compiled classes in the following directory").
By having "classes" in the classpath, the java compiler can find the other classes you may have compiled so far. Keep in view that since there is no "/" in front of classes, the compiler will look for the directory from the directory you invoke it from.
i.e. you should be able to successfully "cd classes" from where you run the javac command!


Cheers,<br />Roger<br />SCJP 1.2, 1.4, SCBCD 1.3, SCWCD 1.4
Paulo Rodriguez
Greenhorn

Joined: Jan 04, 2006
Posts: 1
Hi, everybody.

To solve the problem "javax.servlet.*; does not exist" I did the following:

I copied the file servlet-api.jar from ...\Tomcat 5.5\common\lib to
the directory jdk/jre/lib/ext/

It's work very well!


Best Regards.<br />Rodriguez<br />Be better, be java!
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Is there any tool to compile servlet and JSP through well GUI?
Tom Henricksen
Ranch Hand

Joined: Mar 23, 2004
Posts: 136

netbeans or eclipse are some very good gui's to run these examples through.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Command line arguments