I'm relatively new to Java. I've been using Eclipse to do some work but I want to get back to basics and just use a text editor in conjunction with the JDK. The problem I'm now having is that Eclipse and other IDEs hide away a lot of fundamental stuff which is very important to know and fully understand. This is what I'm trying to do:
I've created a directory called "C:\Java Projects", under which I have created 3 sub-folders, Project1, Project2 and SharedJars. Project1 and Project2 both have SubDirs like classes and source. The Poject1 source .java files live in "c:\Java Projects\Project1\source\com\myApp"
Both Project1 and Project2 are packages which use the Log4J JAR which lives in the SharedJars folder. In the Eclipse world, I could set something which told Eclipse which JARs my Project will use and then do something like import org.apache.log4j.Logger which worked fine. But I'm struggling to get this working.
I've set the CLASSPATH environment variable like "C:\Java Projects\SharedJars\log4j-1.2.15.jar"
I then do the following:
cd Project1\source
javac -d ..\classes com\myApp\*This produces a whole bunch of related errors like
Picked up _JAVA_OPTIONS: -Duser.home="C:\Java Projects"
com\myApp\Monitor.java:11: cannot find symbol
symbol : class Logger
location: class com.myApp.Monitor
private static Logger LOG;
Some questions: 1) Do i still need to import org.apache.log4j.Logger? 2) If so, what determines the FQDN of the package? 3) Do I need to be in a specific directory in order to run javac? Currently i'm going into the source directory of Project1 (the java/bin is in my PATH already)
Sorry for these silly questions. I've trawled through so many websites but many do not cover the very basics. I hope this makes sense.
Rgds John
Yes you do, and that should fix the error in your question, because your
CLASSPATH
is correct.The
import
does not change the FQDN of the package. It just allows you to refer toorg.apache.log4j.Logger
simply asLogger
within that file.No, but it's easiest if the current directory is the
source
directory of the project you are compiling (if it was not, you could work around it with the-sourcepath
option tojavac
.)As for running the program, you might want to create a
Project1.jar
and use the manifest to tell it where to findlog4j.jar
, but that's a separate question.Welcome to SO.
In answer to this:
Yes you always need to use import directives any class you want to use. Java won't load anything even if it is on the classpath unless you tell it to.
Inside the jar, the .class files are located in subdirectories like this:
org/apache/log4j ...
etc. This is how the FQDN is determined and it is used basically as a namespace construct so you can have two or more classes of the same name - which is also why Java can't load everything on the classpath. If it did, it would have no way to disambiguate and work out which class you wanted.No. You can run this from anywhere provided the classpath, from whereever you are, can get to your dependencies. It could be a relative classpath if specified on the command line or absolute if in an environment variable. Likewise you don't need to be in a specific directory to use
java
but you do need it to be able to get to the correct location.I post my solution because none of above solutions were suitable for me. It works for me under Windows XP/7.
SOLUTION
Go to command line and check if everything works by trying example from log4j short tutorial (attachede below):
CONTEXT: Prepare program that uses some *jar file [1] (problem source):
// 1. Go to your source directory (e.g. c:\Users\Hopbit\log4j )
// 2. Create below Bar class and save as Bar.java
// 3. Create below MyApp2 class and save as MyApp2.java
// 4. Create & save MyApp2-log4j.properties file which contains below code:
// 5. Try to compile above example, it should return something like this:
LINKS & HINTS
[1] http://logging.apache.org/log4j/1.2/manual.html ("Short introduction to log4j: Ceki GŁlcŁ, March 2002")
[2] http://logging.apache.org/log4j/1.2/download.html
[3] Above solution was founded in file INSTALL that is placed uder log4j package.