How to compile servlets from command prompt? - java

I'd like to compile a very basic servlet from command prompt, but it is always unsuccessful and the compiler tells me the following:
error: package javax.servlet does not exist.
I googled for the solution and I found that I need to include the servlet.jar libraries into my PATH.
I believe I did.
I strongly believe that the location for those libraries in my computer is:
C:\apache-tomcat-7.0.23\lib\servlet-api.jar\
and the end (the relevant part) of my PATH is the following:
%JAVA_HOME%\bin;C:\apache-tomcat-7.0.23\lib\servlet-api.jar\
For me, it looks ok, but it is obviously not. Can anyone tell me what could be the problem?

classpath not path ... and you don't need it as an evironment variable.
You can set the classpath for javac with option -cp or -classpath (several other ways are also available).
javac uses the environment variable CLASSPATH to look for classes, that can be useful and can also be a source for hard-to-track-down-problems.
An example to compile a java file that uses a library(that is classes from outside the standard JavaSE) would be:
javac -classpath C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java
if your environmental variable CLASSPATH contains libraries you need you might want to do:
javac -classpath %CLASSPATH%;C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java
(please be aware that I don't have access to a windows machine, and therefore haven't tested the idiosyncratic parts of the syntax above)
(also note that in this example "C:\apache-tomcat-7.0.23\lib\servlet-api.jar" is a jar file and not a directory which it might be from your question on your machine)
For command line compiling on windows OS it is a good idea to have the environmental variable JAVA_HOME set correctly and the bin directory of the JDK in the PATH.
I do however suggest getting to write-compile-execute-deploy via/in an IDE for servlet development before figuring out how to do it with just the JDK from a command line.
Java Servlets are not stand-alone executable classes but needs to be deployed into for example tomcat to be tested/used.

First copy the servlet-api.jar file from following path
C:\apache-tomcat-7.0.23\lib\servlet-api.jar;
and paste it in the bin folder of java software by following the path
C:\java\jdk1.6\bin;
Hope now you can successfully compile your servlet program.

1.You can copy your javax.servlet.jar in the jdk1.6\lib folder.
2.You can go to Control Panel\System\Advanced System Properties\Environment Variables
Enter the location of the jar in the CLASSPATH variable as follows:
Then compile and run the servlet.

Related

java classpath and package issues

I have a simple MyHelloWorld.java file within a directory myhelloworld. I set the classpath to appropriate direcotries by exporting CLASSPATH='[some needed class]' in my ~/.bashrc file. and I still need to use javac MyHelloWorld.jaca -cp 'the above classpath' to compile the .java file.
So here's my first question, why do I still need to explicitly set classpath by having a -cp option given the CLASSPATH is already specified?
After the file was compiled, I saw no file like MyHelloWorld, but the java commmand can autocomplete 'java MyHelloWorld' when in fact there's no file named MyHelloWorld exists within the directory? (If I typed "java MyH", the command can be automatically completed).
When I tried to execute 'java MyHelloWorld', it prompted me there's NOClassDefFoundError, which was an indicator of missing library. However when I tried to do 'java -cp MyHello', The command wouldn't complete itself, suggesting it couldn't find anything executable at all
So what are the reasons for above confusing signs. Can anyone take from here and explain to me how java deal with classpath and package, etc. Thanks!
Follow the java tuturial and especialy this section.
The section about JARs is for you too.

Compiling Java package throws errors for external Jars

Pretty basic problem here. So I have a Java package that I have created that has three classes (one has the main method). I am trying to use a few Apache Jars, and have added these to my build path in Eclipse. However Eclipse wont let me build and run it properly, so I am trying the command line. I have added the env var CLASSPATH and pointed it to my lib directory which hold the Apache Jars. However, when I try to use javac I get a bunch of errors:
package org.apache.xmlrpc does not exist
import org.apache.xmlrpc.client.XmlRpcClient;
I was reading the man page for javac and it said that:
If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.
So I tried copying the Jars to the same location as my three source files, but no change.
Can someone please tell me what I'm doing wrong?
Thanks.
Classpath variable (or command line option of javac) must contain all jars explicitly. It cannot go through jar files stored in specified directory.
You can compile this by specifying the option -cp on the command line:
javac -cp foo.jar:bar.jar foo/bar/Baz.java
You then run it with the same option:
java -cp foo.jar:bar.jar foo.bar.Baz
It sounds like you've just set the classpath to the directory containing the jar files. You need to set it to the individual jar files, or use java.ext.dirs to set an "extension" directory containing jar files. I'd recommend using the specific jar files. Something like:
// Assuming Windows...
CLASSPATH = c:\libs\foo.jar;c:\libs\bar.jar
I'd also personally recommend specifying the classpath on the command line instead of using an environment variable - the latter will work, but it ends up being a bit more fiddly if you want to compile different projects against different libraries.
However, I'd actually recommend getting Eclipse working first, rather than retreating to the command line. It should be fine - if you could give us more information about what's failing in Eclipse, we may be able to help you with that instead.
The jar files in the current directory are not automatically included; that only refers to .class files in normal package/directory hierarchy. Jar files must be added either explicitly, or via a wildcard like javac -cp ./* (Assuming JDK6+)
(Some OSes may require an escape of the * to avoid globbing; OSX does not.)
I agree with previous answers, but I would also recommend to use proper java build tool - like ant (perceived easier to use, but not necessary) or maven ( perceived more difficult to use, but really worth learning )

java: importing, class path, and packages

I have a file which imports org.w3c.dom.Document. Compiling and running is fine, but I don't understand how it knows where to find this package and I'm just curious how it works. I used the locate command to try and find org.w3c.dom but I get nothing. Where are these packages located? It seems to me that the right place to look would the CLASSPATH environment variable since my search results seem to be suggesting that. Is this correct? In any case, I don't know how to find out what my CLASSPATH variable is. It doesn't seem to be an environment variable that my shell knows about.
That would be part of the core libraries (rt.jar), so it'd be wherever you installed the java JRE; specifically under $JAVA_HOME/jre/lib
You can look inside the .jar files using the jar command. To see the class you mention, you can do:
jar tvf rt.jar
This lists all the classes in that jar.
Note that this location is automatically searched by the JVM - it's not needed nor included in the CLASS_PATH environment variable. (You could add it, but it would simply be redundant)
Edit for clarity:
The JVM includes <Where_you_installed_jdk>/jre/lib and <Where_you_installed_jdk>/jre/lib/ext by default. Anything else has to be explicitly added by you via either passing it to java directly via the -cp option or adding it to the CLASS_PATH environment variable.
The relavent documentation can be found at: http://download.oracle.com/javase/6/docs/technotes/tools/findingclasses.html
The JVM finds classes using classpath settings where alll paths to required packages are set. The classpath could be set with a number of ways. The first mentioned by you is CLASSPATH environment variable. It is optional and can be unset. The second way is an explicit option "-cp" for "java" executable.
Also some JRE runtime jars are added to classpath by default implicitly so you don't need to search and add standard packages by yourself (particulary the one you mentioned in your question).
try compiling messconvener.java like this from its own directory
javac -d ..\..\. -cp ..\..\. messconvener.java
-d - creates directory structure for your package
-cp - provides class path for user file, where it can find user defined classes

Java - Difficulty installing program from 3 separate .jar files (involves CLASSPATH)

I'm having a little trouble running some Java code, which requires three .jar files to be used. I'm at a lost as to what to do with them--I've tried setting the CLASSPATH (and following the instructions for how to do so in the readme files), but to no avail.
I was wondering if someone could walk me through it? I'd imagine three .jar files would be an easy install for someone who knows what they're doing.
If it helps, I'm using Ubuntu pretty much right out of the box (but I do have JDK and Eclipse installed!)
Runtime library: http://cogcomp.cs.illinois.edu/download/software/20
Additional .jar needed: http://cogcomp.cs.illinois.edu/download/software/23
Program I ultimately need to run: http://cogcomp.cs.illinois.edu/download/software/26
If you're willing to help, I can't thank you enough--you deserve a million kudos!
G
Those are all JAR files. When you execute a JAR file by doubleclicking or using java -jar, the CLASSPATH environment variable and the -cp and -classpath arguments are ignored. The classpath should be defninied in META-INF/MANIFEST.MF file of the JAR. In this particular case, only the second and third JAR have a Class-Path entry in the manifest file:
Class-Path: LBJ2Library.jar
Which is the first JAR. The classpath is telling that it is expecting the LBJ2Library.jar to be in the same folder as the JAR you'd like to execute (either the second or third one).
So, just drop them all in the same folder and execute by java -jar LBJPOS.jar.
If you are using java -jar to run your jar files, then the CLASSPATH variable is ignored. If you are using java -jar, you have two options:
Combine the three jars into one jar.
Run the main class directory and don't use -jar.
Use of the CLASSPATH environment variable is generally discouraged nowadays. This is how it's done (on Linux):
java -cp library1.jar:library2.jar:mainapp.jar <fully qualified name of main class>
You need to set the CLASSPATH .place all the 3 jars in a folder , name it as lib
See below to set classpath
set CLASSPATH=%CLASSPATH%:lib;

How can I find a package?

In my code I have the following statement import com.apple.dnssd.*; and compiler (javac) complains about this line. It writes that the package does not exist. But I think that it could be that "javac" search the package in a wrong place (directory). In this respect I have two questions:
How can I know where javac search for the packages?
I think that it is very likely that I have the above mentioned package but I do not know where it is located. What are the typical place to look for the packages?
ADDED:
On another Windows machine I tried the same thing and the "javac" does not complain (as before I compiled without any options like "-cp"). I check values of the "classpath" environment variable. It is equal to "C:\Program Files\Java\jdk1.6.0_18\bin;.;..". I went to the first classpath directory and did not find there something that could be the "com.apple.dnssd" library (no jar files, no files containing "apple"). So, I do not understand why javac do NOT complain on the second Windows machine.
ADDED 2:
On the machine #2 I have installed Bonjour after JDK. On the machine #1 JDK was installed after Bonjour.
ADDED 3:
On the machine #1 (where I cannot import the package) I found the jar file (it is located in "C:\Program Files\Bonjour" and its name is "dns_sd.jar"). I tried to add the above mentioned directory to the PATHCLASS environment variable on Windows 7 (and I restarted the system). It does not help. I still cannot import the package. I also tried to specify the "-classpath" in the command line. It also does not help. Now I will try to reinstall Bonjour (as it was advised).
ADDED 4:
I have uninstall Bonjour and Bonjour SDK. I have reload Window. Then I have installed Bonjour and Bonjour SDK. I have reload the Window. It did not solve the problem. I still cannot import the package (javac writes that package does not exist). I have also copied the *.jar file to the same directory there the source is located. It does not work. I used "javac -cp .". It does not work. Now I am out of options. I do not know what else can I try. Can anybody help me pleas?
ADDED 5:
My classpath is: C:\Program Files\Java\jdk1.6.0_18\bin;.;..;"C:\Program Files\Bonjour"
I try to compile from this directory: C:\Users\myname\java\bonjour\example
I compile by the following command: javac ServiceAnnouncer.java
I get the following error message: ServiceAnnouncer.java:1: package com.apple.dnssd does not exist
ADDED 6:
Finally I have managed to import the library. I did it in the following way:
javac -cp "C:\Program Files\Bonjour\dns_sd.jar" ServiceAnnouncer.java
The important thing is that I have specified the jar file after the -cp (not the directory where the jar file is located). It works also if I replace "dns_sd.jar" by "*". So, my conclusion is that after the "-cp" I need to specify jar files (not directories).
Java/javac will search for classes in the classpath.
The default classpath covers the /path/to/jre/lib and /path/to/jre/lib/ext folders. Any classes and JAR files which are found there will be taken in the classpath. You can in theory put your classes and JAR files there so that you don't need to do anything to get java/javac to find them. But this is actually an extremely bad practice. It's recipe for portability trouble, because this isn't the same in all machines. Leave those folders intact.
Then there's the environment variable %CLASSPATH% wherein you can specify full paths to root folders where classes are located and/or full paths to JAR files (including the JAR file name itself!). Multiple paths are in Windows to be separated by semicolon ; and in *Nix by colon :. Paths with spaces inside needs to be quoted with "". Here's an example:
SET CLASSPATH = .;/path/to/File.jar;"/spacy path to some pkg/with/classes"
Note the period . at the beginning of the argument. This indicates the current path (the current working directory from where the java/javac command is to be executed). It will only find classes in the current path that way, and thus not JAR files! You need to specify full path for them. Since Java 1.6 you can also use wildcards to specify multiple JAR files in some path. E.g.
SET CLASSPATH = .;/path/to/all/jars/*;"/spacy path to some pkg/with/classes"
This environment variable is actually a convenience way to manage the classpath so that you don't need to type the same thing down again and again in the command console everytime. But this is only useful for new-to-java users and the cause of all future confusion because they will think that this is "the" classpath. This assumption is actually wrong and again the cause of portability trouble because this isn't the same in all machines.
The right way to define the classpath is using the -cp or -classpath argument wherein you actually specify the same information as you'd like to enter for %CLASSPATH%, i.e. (semi)colon separated and paths-with-spaces quoted, for example:
javac -cp .;/path/to/File.jar;"/spacy path to some pkg/with/classes" Foo.java
Note that when you use either -cp or -classpath (or -jar) arguments, then java/javac will ignore the %CLASSPATH% environment variable (which is actually a Good ThingTM).
To save the time in retyping the same again and again, just create a bat or cmd file (or if you're on *Nix, a sh file). Basically just put therein the same commands as you'd enter "plain" in the console and then execute it the usual platform specific way.
To save more time, use an IDE. The classpath which is to be used during both compiletime and runtime inside the IDE is called the "build path". Explore the project properties and you'll see.
http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html
To answer your first question (How to know where javac searches for packages):
Check what your $CLASSPATH variable is set to.
echo $CLASSPATH
This is where you JRE will search for class files and resources. You can either set it as an environment variable,
set CLASSPATH=path1;path2 ...
or set it when your run javac.
C:> javac -classpath C:\java\MyClasses src_dir
(Great examples for javac are found here)
In this case, your jar file containing 'com.apple.dnssd.*' should be located in your classpath. Just download that jar, and put it in the place where your classpath is searching.
Assuming that dns_sd.jar is installed in 'C:\Program Files\Bonjour', then try to compile your code like this:
cd C:\Users\myname\java\bonjour\example
javac -classpath C:\Program Files\Bonjour ServiceAnnouncer.java
This link suggests that the JAR containing this package is part of Bonjour for Windows. Look for it there.
javac.exe only searches where you tell it with the CLASSPATH. If you don't understand how to set CLASSPATH, I'd recommend reading something like this.

Categories