Setting classpath for a jar and a class - java

I need to set the classpath for servlet-api.jar and another class in order to compile a file.java.
How could I accomplish that? I have tried
javac -cp /path/to/servlet-api;/home/user/Desktop/Other.class file.java
However it does not work.
Any help? I am aware it's possible to set the environment variable however I would like to know if it's possible manually. Thanks

The classpath must contain jar files, and directories. Directories must be directories containing the root of a package tree. So, assuming Other is in the package com.foo.bar, and its class file is /home/user/Desktop/com/foo/bar/Other.class, the classpath should be
-cp /path/to/servlet-api.jar:/home/user/Desktop
Note that : is the path separator on Unix. ; is for Windows. I assume you're not on Windows since your path is /home/... and not c:\home\...
PS: If Other is not in any package, then fix that. Classes should always be in a package.

Related

How to import JAVA packages in cmd?

This is the first time I am compiling a program, and it doesn't seem to be working out. Looks like some packages are not being located - so for this question, I'll just focus on one:
Steps I've take so far:
1) setting up the System Variable Path to include java
2) in CMD.exe: jar tf log4j.jar I did this to make sure it includes log4j.Logger and it does.
3) I Shift+rightclick and open command prompt from this folder:
4) Then I enter javac TNT.java and i get the following error (along with others):
Any thoughts?
I set the classpath to the same folders with set classpath = "name of folder" no change...
edit
5) have also tried
javac -cp jdkbindirectory;jrebindirectory;theabovefolder TNT.java
I get this:
blahblahblah
You shouldn't set the classpath using an environment variable as it is bad practice. What if you accidentally change it later for a different project and your current project breaks?
When including classes in the classpath, you can include the path of the root of the package of the class, as in the folder that contains the folders in the package structure. However, when you're including a jar in your classpath, you need to put the entire path of the jar file (relative to the current working directory) all the way up to the jarname.jar.
Also, remember that by default, java looks in the current working directory and uses that as its default classpath. However, as soon as you specify a classpath it no longer does that automatically for you. Be sure that you're including your current directory in your classpath as well.
Finally, be sure to surround the classpath in quotes otherwise java might think its a part of another argument.
I would try this:
javac -cp "./;log4j.jar" TNT.java
And then to execute the class file:
java -cp "./;log4j.jar" TNT
Hope this works, good luck!

How to add a class to classpath to not get import errors in Java [duplicate]

I have two classes:
MyApplication Library
The Library has already been compiled into Library.class and the source code is no longer available. I am now trying to compile MyApplication from source. MyApplication depends on the Library. The Library has a package name of org.myCompany. I tried setting my classpath to the following:
set CLASSPATH=C:\java\project\org\myCompany\Library.class;.
which produced the following javac compiler error message:
MyApplication.java:33: cannot find symbol
symbol: class Library
location: class MyApplication
Library theLibrary = new Library();
So I changed my classpath to be:
set CLASSPATH=C:\java\project\;.
which produced the exact same error message.
How do I set my Windows classpath to include the Library.class file? Should it point at the folder contains the org\myCompany subfolders? Or point directly to the class file? Or to the folder containing the class file (even though the class is in a package and belongs in a subfolder)?
I do an echo %CLASSPATH% after my set command and the classpath is being set correctly. I also made an ant build.xml file and encountered the same problem. In fact, ant -verbose confirmed that my classpath is being set correctly.
First of all: the use of the CLASSPATH environment variable is very strongly discouraged. The best thing is for you to forget that it exists. Use the -cp command line switch or similar methods to set the classpath.
Second, the classpath entries each represent a place where the classloader will start looking for .class according to the package hierarchy, i.e. it will look for the class org.myCompany.Library in a subfolder org/myCompany in any of the classpath entries.
Therefore, if
you add a classpath entry C:\java\project\
and there is a class file C:\java\project\org\myCompany\Library.class
which is actually part of a package org.myCompany (capitalization matters here!)
and your MyApplication class has an import org.myCompany.Library;
Then it really should work.
You cannot add a single class in your classpath like this. You have 3 solutions:
add this class in the path of your other compiled classes (respecting the package naming of your directories)
add the root directory of this class in your classpath (in your case "C:\java\project\")
add this single class into a jar and add this jar to the classpath
For your problem, the thrird choice is cleaner: external dependencies normally are packaged into jar files.
If your .class file isn't in jar file, point your classpath to the parent dir where package of class resides, e.g., for class org.myCompany.Library, point your CP to directory containing org/myCompany.
If your .class file included into some jar file, add full path to that jar to your classpath.
If you compiled the class files to a different directory, the classpath needs to point to where the .class file is.
set CLASSPATH=C:\java\project\;
is correct assuming that that the class file is in the same directory as the .java source file.
Is there a problem locating the Library to the same root project where is your MyApplication class
Example, if:
c:/project/org/company/MyApplication.class
Can you locate the Library class into:
C:/project/org/myCompany/Library.class
please notice, that the folders org/myCompany and org/company are located under the same folder c:/project/.
Please notices that this solution works for you if the Library Class is only used by your application.
Edited
Windows command prompt is tedious, after setting the classpath please close and re-open the Command Prompt, so it can see the new classpath's value.
For the classpath to work, you need to have a folder structure which matches the package hierarchy. So if your class is org.myCompany.Library, you must create a nested folder structure of C:\java\project\org\myCompany and place your Library class file in the myCompany folder. Then set the class path to C:\java\project\

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

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.

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.

Java: class resolved?

I hope this question is not repeated. But just can't find answer anywhere:
I have ONE folder containing two files one A.java another B.class.
Now in A.java I am trying to declare
public class A extends Applet{
...
B aB;
}
The compiler gives me:
B cannot be resolved to a type
I read a lot of posts that say if the files are in the same folder, I don't need to import. Could anyone help me to "resolve" this problem?
Thanks much appreciated!
-----------SOLVED! - SEE ANSWER BELOW------------------
The .class files need to reside in a directory referenced by the classpath variable. Usually you put your .java files in one directory (src), compile to another directory (bin) and have external .class files in a third directory (lib). The commands will look like this:
# compile
javac -sourcepath src -classpath lib -d bin
# run
java -classpath bin:lib A
Using an IDE like eclipse should help a lot here as it takes care of most of the details
The simple case that you've posted works for me. I'd check the following things:
Are you sure that B.class is present in the same folder as A.java?
Are you running javac from that folder?
Have you typed the class name B correctly everywhere in your program? This includes capitalization, as Java identifiers are case sensitive.
Are there any package declarations in your program? If there are, none of this is going to work, since you're implicitly using the default package by just throwing everything into a folder.
The compiler looks for *.class file in its class path. It will only look for *.java files in the same source directories. You need to set the class path to include the directory.
Or you could use an IDE which sets all this up for you and saves a lot time in the process.

Categories

Resources