tags:

views:

79

answers:

2

Hi,

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

+1  A: 

1) Do i still need to import org.apache.log4j.Logger?

Yes you do, and that should fix the error in your question, because your CLASSPATH is correct.

2) If so, what determines the FQDN of the package?

The import does not change the FQDN of the package. It just allows you to refer to org.apache.log4j.Logger simply as Logger within that file.

3) Do I need to be in a specific directory in order to run javac?

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 to javac.)

As for running the program, you might want to create a Project1.jar and use the manifest to tell it where to find log4j.jar, but that's a separate question.

finnw
Ok so I add the line: import org.apache.log4j.Logger; It then comes back with this..... "package org.apache.log4j does not exist"Thanks for your reply
Wilko
@John Wilkinson, It may be that you set the environment variable incorrectly. Try moving the classpath to the command line, e.g. `javac -classpath "C:\Java Projects\SharedJars\log4j-1.2.15.jar" -d ..\classes com\myApp\Monitor.java` and see if that makes a difference.
finnw
That's it! I need to understand why my CLASSPATH var is not being evaluated correctly but that's another issue. Many thanks to all reponses
Wilko
+3  A: 

Welcome to SO.

In answer to this:

Do i still need to import org.apache.log4j.Logger?

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.

If so, what determines the FQDN of the package?

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.

Do I need to be in a specific directory in order to run javac?

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.

Ninefingers
Many thanks for your reponse. With help from finnw, I'm now setting the -classpath on the javac command explicity citing the log*jar which did the trick
Wilko
Excellent, glad it's now working! I +1'd his answer for his efforts!
Ninefingers
Java can load classes without using an import, you have to fully qualify them every time you use them and they have to be on the classpath, e.g. org.apache.log4j.Logger log = new org.apache.log4j.Logger();
Kelly French
@Kelly true. I did omit to mention that org.apache.log4j.Logger x = new org.apache.log4j.Logger() would work...
Ninefingers