Use my own classes in a servlet (Java/Tomcat/Linux) - java

For the last 3 days I couldn't find a single answer for this problem. I need to be able to use my own classes in my servlets.
I am pretty sure that my files hierarchy is correct:
|-WEB-INF/
|---classes/
|------com/
|---------myProject/
|------------user/
|---------------User.java
|---------------Location.java
|---------------Comment.java
|------------servlet/
|---------------DoComment.java
Since User.java, Location.java and Comment.java are defined in one package as com.myProject.user I know I should go to the main root of the java project and compile them this way:
/var/lib/tomcat6/webapps/ROOT/WEB-INF/classes$ sudo javac com/myProject/user/Location.java
/var/lib/tomcat6/webapps/ROOT/WEB-INF/classes$ sudo javac com/myProject/user/User.java
/var/lib/tomcat6/webapps/ROOT/WEB-INF/classes$ sudo javac com/myProject/user/Comment.java
However, javac cannot identify the other objects (cannot find symbol error) when I use a classpath in my compilation.
/var/lib/tomcat6/webapps/ROOT/WEB-INF/classes$ sudo javac -cp /usr/share/tomcat6/lib/servlet-api.jar com/myProject/servlet/DoComment.java
Please help!

You're putting java source in a location where compiled java classes are expected. Try compiling the source and then adding the class files (.class vs .java) where you're putting them currently.
For this to work, you'd have to be sure that the classes have no dependencies and that the package declaration of your classes match up with the folder hierarchy you're placing them under.
Even so, this generally isn't how web projects are put together. You would be better off packaging the classes into a JAR and placing the JAR in your WEB-INF/lib folder.
For more information on creating a JAR, check this out: http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

From the javac manpage:
-cp classpath
Sets the user class path, overriding the user class path in the CLASSPATH environment
variable. If neither CLASSPATH or -class-
path is specified, the user class path consists of the current
directory.
Basically, you are overriding the classpath when you use the -cp flag, so you need to make sure you specify ALL the required classes in your classpath. The delimiter for classpath entries is a : and it takes wildcards.

Problem solved!
I created a JAR file for the com.myProject.user package and saved it in the WEB-INF/lib. Than I compiled the servlet using two classpath seperated by a colon.
Here is the code:
/var/lib/tomcat6/webapps/ROOT/WEB-INF/classes$ sudo jar cvf myproject-user.jar com/myProject/user/User.class com/myProject/user/Location.class com/myProject/user/Comment.class
/var/lib/tomcat6/webapps/ROOT/WEB-INF/classes$ mv myproject-user.jar ../lib/myproject-user.jar
/var/lib/tomcat6/webapps/ROOT/WEB-INF/classes$ sudo service tomcat6 restart
/var/lib/tomcat6/webapps/ROOT/WEB-INF/classes$ sudo javac -cp /usr/share/tomcat6/lib/servlet-api.jar:../lib/myproject-user.jar com/myProject/servlet/DoComment.java
Thank you durron597 and kwikness your answers combined was the correct answer.
Have a nice day.

Related

Compiling a java file using javac and the command line

I am trying to learn more about javac and how to use developer tools for Java using the command line.
As far as I understood, the option -classpath is needed to specify the path where javac searches for our classes and resource files, if we are not in the current directory, because usually the class path is set to our current working directory.
This is my current working directory:
/Users/user1/Desktop
And I am trying to compile a .java file which is in:
/Users/user1/Desktop/PF/
and the file is called MainClass.java.
I am trying to compile it using the following command:
javac -classpath /PF MainClass.java
But it does not seem to work, in fact I keep receiving the following:
javac: file not found: MainClass.java
Usage: javac <options> <source files>
use -help for a list of possible options
What am I doing wrong?
Classpath is for .class files, not for .java files.
javac command needs correct path to the .java file to compile it. So
javac ./PF/MainClass.java
Will create the class file in current directory.
If your MainClass.java depends on any class files to compile correctly, then you put those class/jar files in classpath.
That isn't how the classpath works. You use the classpath to point to classes that your Java file needs in order to compile. You don't use the classpath to point to the Java file itself.
Either go into the PF directory and do this:
javac MainClass.java
That will create the MainClass.class file inside the PF directory. If instead you want to create the MainClass.class file on your desktop, then from your desktop, do this:
javac PF/MainClass.java
-classpath
Specifies the path javac uses to look up classes needed to run javac
or being referenced by other classes you are compiling. Overrides the
default or the CLASSPATH environment variable if it is set.
Directories are separated by colons. It is often useful for the
directory containing the source files to be on the class path. You
should always include the system classes at the end of the path.
class path is used to specify the compiled sources that need to be used in your class. For example in this code if you are accessing another class then you should specify the location of the compiled sources of the that class.
In your case if don't have any class dependency then simply remove classpath option and compile using[navigate inside folder]
javac Mainclass.java
Remove the -classpath. And if you are in the place where the java file is required (which currently you arent) you can remove that PF/ too.

Is it possible to use relative path when compiling the java files?

Hi I am trying to learn to compile a java class with make file. My make file looks like this:
build:
javac test_java.java
clean:
rm -rfv *~ test_java.class
run:
java test_java
Now I have moved the test_java.java into a folder, called classes I am trying to compile the file using a relative path, is it possible?
I have tried:
javac -d classes test_java.java
but I am getting errors:
javac -d classes test_java.java
javac: file not found: test_java.java
Usage: javac <options> <source files>
use -help for a list of possible options
I have also tried: -d ./classes and also -d /home/the/whole/path (but I would like to have the relative path), and the errors are the same. It seems to work only the
javac classes/test_java.java
What am I doing wrong?
Update: The original answer here was completely wrong. You must specify the path to your source files. -sourcepath is for another purpose:
Specify the source code path to search for class or interface
definitions. As with the user class path, source path entries are
separated by semicolons (;) and can be directories, JAR archives, or
ZIP archives. If packages are used, the local path name within the
directory or archive must reflect the package name.
Note that there are many other build tools for Java applications that are very mature and well regarded. Maven, Gradle, and Ant are the ones that spring to mind immediately. If you don't have to use make I would take a look at one of those.

Could not find or load main class , environment variables

I know this has come up a number of times, but previous responses just don't seem to help.
My environment variables are :
CLASSPATH C:\Program Files\Java\jre7\lib;C:\Program
Files\Java\jdk1.7.0_15\bin;
PATH C:\Program Files\Java\jdk1.7.0_15\bin;
When moving to the directory as follows C:\Users\Oli\My Documents\java I can compile using javac, but cannot runt he program using java. I know its most likely got something to do with environment variables but I cannot get it to work. P.S the error is "could not find or load main class"
Any help would be appreciated.
CLASSPATH is the place where JRE looks for classes. You've set your CLASSPATH to a value and expect to run the class from current Directory, which won't work.. for instant solution you may use
java -cp C:\Users\Oli\My Documents\java ClassName
Or undo setting CLASSPATH. Default CLASSPATH is current directory
Lets assume that your ".java" file default package ( no package defined) survivies in "C:\Src"
You dont need to set the CLASSPATH in this case.
cd C:\Src
javac MyJava.java
java MyJava
If with package say com.test
cd C:\Src
javac com\test\MyJava.java
java com.test.MyJava
However if you are not in the same folder as Source files and want to run from anywhere
set CLASSPATH=%CLASSPATH%;C:\src
javac MyJava.java or javac com\test\MyJava.java
and
java com.test.MyJava or java com.test.MyJava
Unset CLASSPATH and just use the default one provided by the JVM. Here is a link to the Java Tutorial that covers the environment variables.
Seems like the problem is not in the path...
Does your code use the 'package' statement? (i.e. package my_package;)
If so, go to 'java' directory and execute:
java my_package.MyClass
where 'my_package' is the name of... the package, and MyClass is your compiled .java file (without the .class extension).
Good luck.

Package not found; javac

This is annoying.
I have a directory structure like this
-lib
--some jar files
-packageName
--Main.java
--SomeOtherPackage
--SomeOtherJavaClass.java
Main.java imports SomeOtherPackage. And both java files uses jars in the lib.
What I do is add the jar files independently in the CLASSPATH. And then run as:
javac packageName/Main.java
but it gives the error that Package not found SomeOtherPackage . Shouldn't it automatically realize the dependency and build SomeOtherPackage as well? What would be the javac command and the classpath for the above case?
Thanks
The normal practice is to add the package root to the classpath.
When you're already in the package root, use -cp .. E.g.
cd /path/to/all/packages
javac -cp . packageName/Main.java
If you want to include JAR files as well, use the ; (or in *nix, the :) as classpath path separator:
javac -cp .;lib/file.jar packageName/Main.java
To save the time in repeating all the typing of shell commands, use a .bat (or in *nix a .sh) file. Or just an IDE if you're already familiar with java/javac and so on.
You need to add packageName to the CLASSPATH so it can find SomeOtherPackage

Making java packages

My Java classes organization has gotten a little messy so I'm going over something I've skipped in my Java learning: the classpath. I can't quiet get beloved classes to compile in the packages I have created for them. Here's my folder hierarchy:
.
com/
david/
Greet.java
greeter/
SayHello.java
SayGoodbye.java
Where SayHello's constructor just prints "hello" and SayGoodbye's prints "Goodbye" and Greet's main method just creates those two objects. At the top of SayHello is package com.david.greeter; and likewise with SayGoodbye and Greet's is package com.david;
In the greeter folder I was able to compile both java files but if I go to the current directory (the directory that holds com) and do javac -cp "com.david.greeter.*" com/david/Greet.java it says it can't find the classes as well as saying package com.david.greeter doesn't exist. I've also tried setting the $CLASSPATH manually.
I'm at my wit's end here, Stackoverflow (as I normally am when I post here). Do any of you know what I am doing wrong?
The java compiler will traverse the sub-directories of the classpath looking for the packages it needs.
So, your command line should be as follows:
javac -cp "." com/david/Greet.java
When the compiler sees a reference to com.david.greeter.SayHello while compiling Greet.java it will start with the directory in the classpath and traverse the hierarchy looking for the package it needs.
First, as documented in Setting the Classpath, the way you're currently setting your class path is wrong. Class path entries should be filename or directory. So using com.david.greeter.* doesn't make any sense. Second, the current directory is in the class path by default:
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
So if you execute javac (here is the man page by the way) from the folder containing com, you don't have to tweak anything, just type:
javac com/david/Greet.java
And javac will go through the directory tree to find references (e.g. SayHello if you're using it from Greet) and compile them too.
And by the way, if you have to set the class path, don't use the $CLASSPATH environment variable, this is just a bad practice in most case, prefer the -cp option.
If you are in the folder containing com, then try this:
javac -cp . com\david\Greet.java
This is incorrect (as the compiler has already told you):
javac -cp "com.david.greeter.*
Open a command shell and navigate to the directory that contains the "com" directory.
I think you really want this to compile SayHello.java and SayGoodbye.java:
javac -cp . com/david/greeter/*.java
This to compile Greet.java:
javac -cp . com/david/*.java
And this to run:
java -cp . com.david.Greet
The "com" directory should not be current, it should be a child directory to current. You need step one level upper and launch again. No extra care about classpath should be needed at this point.

Categories

Resources