Javac erroring out when compiling Servlet libraries - java

I am using ubuntu and I have set my paths to be the following:
JAVA_HOME=/usr/local/jdk1.6.0_24
export CLASSPATH=/usr/local/tomcat/lib
export JAVA_HOME
I thought that would put the servlet libraries in the compile path, but I am still getting compile errors like this:
package javax.servlet does not exist
[javac] import javax.servlet.ServletException;
Any ideas how to fix this or what I am doing wrong? The general Java libraries seem to be working fine.

With jar files, simply specifying a directory containing jar files will not work. You have two options:
Specify each jar file individually on the CLASSPATH:
export CLASSPATH=/usr/local/tomcat/lib/servlet-impl.jar:/path/to/another.jar
Since you're using Java 6, you should be able to use wildcards (to include all jars in a directory):
export CLASSPATH=/usr/local/tomcat/lib/*

Related

Can a Java class import classes from a third-party project with dependencies managed by Gradle? [duplicate]

I read the Java tutorials on Sun for JAR files, but I still can't find a solution for my problem. I need to use a class from a jar file called jtwitter.jar, I downloaded the file, and tried executing it (I found out yesterday that .jar files can be executed by double clicking on them) and Vista gave me an error saying "Failed to load Main-Class Manifest attribute from [path]/jtwitter.jar".
The guy who coded the .jar file wants me to import it, but where do I store the .jar file to import it in my code? I tried putting both the .jar file and my .java file in the same directory, didn't work.
The file I'm trying to work for is here: http://www.winterwell.com/software/jtwitter.php
I'm using JCreator LE.
Let's say we need to use the class Classname that is contained in the jar file org.example.jar
And your source is in the file mysource.java Like this:
import org.example.Classname;
public class mysource {
public static void main(String[] argv) {
......
}
}
First, as you see, in your code you have to import the classes. To do that you need import org.example.Classname;
Second, when you compile the source, you have to reference the jar file.
Please note the difference in using : and ; while compiling
If you are under a unix like operating system:
javac -cp '.:org.example.jar' mysource.java
If you are under windows:
javac -cp .;org.example.jar mysource.java
After this, you obtain the bytecode file mysource.class
Now you can run this :
If you are under a unix like operating system:
java -cp '.:org.example.jar' mysource
If you are under windows:
java -cp .;org.example.jar mysource
Not every jar file is executable.
Now, you need to import the classes, which are there under the jar, in your java file. For example,
import org.xml.sax.SAXException;
If you are working on an IDE, then you should refer its documentation. Or at least specify which one you are using here in this thread. It would definitely enable us to help you further.
And if you are not using any IDE, then please look at javac -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,
java -jar my_program.jar
you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.
You need to add the jar file in the classpath. To compile your java class:
javac -cp .;jwitter.jar MyClass.java
To run your code (provided that MyClass contains a main method):
java -cp .;jwitter.jar MyClass
You can have the jar file anywhere. The above work if the jar file is in the same directory as your java file.
You need to put the .jar file into your classpath when compiling/running your code. Then you just use standard imports of the classes in the .jar.
As workmad3 says, you need the jar file to be in your classpath. If you're compiling from the commandline, that will mean using the -classpath flag. (Avoid the CLASSPATH environment variable; it's a pain in the neck IMO.)
If you're using an IDE, please let us know which one and we can help you with the steps specific to that IDE.

Installing GSON JAR file and using it with Java [duplicate]

I read the Java tutorials on Sun for JAR files, but I still can't find a solution for my problem. I need to use a class from a jar file called jtwitter.jar, I downloaded the file, and tried executing it (I found out yesterday that .jar files can be executed by double clicking on them) and Vista gave me an error saying "Failed to load Main-Class Manifest attribute from [path]/jtwitter.jar".
The guy who coded the .jar file wants me to import it, but where do I store the .jar file to import it in my code? I tried putting both the .jar file and my .java file in the same directory, didn't work.
The file I'm trying to work for is here: http://www.winterwell.com/software/jtwitter.php
I'm using JCreator LE.
Let's say we need to use the class Classname that is contained in the jar file org.example.jar
And your source is in the file mysource.java Like this:
import org.example.Classname;
public class mysource {
public static void main(String[] argv) {
......
}
}
First, as you see, in your code you have to import the classes. To do that you need import org.example.Classname;
Second, when you compile the source, you have to reference the jar file.
Please note the difference in using : and ; while compiling
If you are under a unix like operating system:
javac -cp '.:org.example.jar' mysource.java
If you are under windows:
javac -cp .;org.example.jar mysource.java
After this, you obtain the bytecode file mysource.class
Now you can run this :
If you are under a unix like operating system:
java -cp '.:org.example.jar' mysource
If you are under windows:
java -cp .;org.example.jar mysource
Not every jar file is executable.
Now, you need to import the classes, which are there under the jar, in your java file. For example,
import org.xml.sax.SAXException;
If you are working on an IDE, then you should refer its documentation. Or at least specify which one you are using here in this thread. It would definitely enable us to help you further.
And if you are not using any IDE, then please look at javac -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,
java -jar my_program.jar
you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.
You need to add the jar file in the classpath. To compile your java class:
javac -cp .;jwitter.jar MyClass.java
To run your code (provided that MyClass contains a main method):
java -cp .;jwitter.jar MyClass
You can have the jar file anywhere. The above work if the jar file is in the same directory as your java file.
You need to put the .jar file into your classpath when compiling/running your code. Then you just use standard imports of the classes in the .jar.
As workmad3 says, you need the jar file to be in your classpath. If you're compiling from the commandline, that will mean using the -classpath flag. (Avoid the CLASSPATH environment variable; it's a pain in the neck IMO.)
If you're using an IDE, please let us know which one and we can help you with the steps specific to that IDE.

Packages not found while compiling java

I'm trying to compile my java file to class.
It is a plugin to a much larger program and it runs fine from eclipse when running from source.
I work in Linux, and in shell I type the following javac MyPlugin.java
This is just some of the output:
MyPlugin.java:11: package javolution.util does not exist
import javolution.util.FastList;
^
MyPlugin.java:12: package javolution.util does not exist
import javolution.util.FastMap;
^
MyPlugin.java:14: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
^
MyPlugin.java:15: package org.jwebsocket.api does not exist
import org.jwebsocket.api.PluginConfiguration;
^
My assumption is that the rest of the errors are caused because the imports cannot be reached.
Do I need to add something to the original command to have these imports included?
You are missing jars from the following projects:
http://javolution.org/
http://logging.apache.org/log4j/1.2/
http://jwebsocket.org/
You can download the missing JARs and include them in your compilation classpath.
Alternatively, you could let maven manage the download and compilation of your application.
Update: Gathering all external jars together in one location
To get all your external libraries in one place you could create a runnable JAR in eclipse using:
Export...->Java->Runnable JAR File
Enter your_temp_jar.jar and click
Copy required libraries into a sub-folder next to the generated JAR
After clicking Finish all the required jars will be in a folder called your_temp_jar_lib.
You should specify the jar files which contains following classes in your classpath.
javolution.util.FastList
javolution.util.FastMap
org.apache.log4j.Logger
org.jwebsocket.api.PluginConfiguration
you can use -cp to specify classpath of jars.
Typing javac at the command line is not a viable or scalable way to build java code. While you could use -cp to javac to add the required dependencies to this compilation, you'd be far better served by learning ant or maven.

how to import jar files in linux machine

I am trying to run a Java program on Linux machine, that includes:
import javax.mail.*;
I have included mail.jar into myjdk/lib. Even on compiling it shows errors like
javax.mail does not exist
What to do?
On bash run this command
export CLASSPATH=$CLASSPATH:/path/to/my.jar
and then compile
Check your CLASSPATH. Make sure it points to all JAR files in the directory. This should be specified by either a direct path to the your JAR, or a wildcard.
Take a look at this page on setting the class path: http://download.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html
Based on your comments, I'm going to assume that you have a script to construct the classpath from the files in the "lib" directory.
In that case, the most likely problem is that Windows uses a semi-colon (";") as a classpath separator, while Linux uses a colon (":").
Offhand, I don't know of a solution that works in both environments. The usual approach is to create a "runme.sh" for Linux, and a "runme.bat" for Windows.
Copy all your jar files and .java files in same folder.
To compile,
javac -cp .:mail.jar SendEmail.java
To execute,
java -cp .:mail.jar SendEmail

Using custom .jar's in your java project

I'm importing from two jar's twitter4j-2.0.10.jar and mysql-connector-java-5.1.10-bin.jar, I'm running the code on a server but it's not letting me. I get a..
Streamer.java:9: package twitter4j does not exist
import twitter4j.StatusDeletionNotice;
^
error when compiling. I know this has something to do with getting java to recognise my custom jar's but I don't know how to get java to "see" them.
Any ideas?
You need to set the classpath for Javac.
One possible example (Windows, with your jar files in a folder called "lib"):
javac -classpath lib/twitter4j-2.0.10.jar;lib/mysql-connector-java-5.1.10-bin.jar MyClass.java
That's a very precise example though, your environment could differ considerably.

Categories