How do I import JARs and Packages in Java

2019-06-17 04:00发布

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

标签: java jar package
3条回答
倾城 Initia
2楼-- · 2019-06-17 04:30

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.

查看更多
淡お忘
3楼-- · 2019-06-17 04:34

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.

查看更多
Deceive 欺骗
4楼-- · 2019-06-17 04:37

I post my solution because none of above solutions were suitable for me. It works for me under Windows XP/7.

SOLUTION

  1. Download zip or tar package from log4j site[1] to your computer & unpack downloaded file.
  2. Copy file log4j-VERSION.jar to your LOG4J_ISTALLATION_DIR (e.g. c:\javalibs\log4j-1.2.16.jar)
  3. Open Properties of "My Computer/Computer" (icon should be placed on your desktop, just click right mouse button on it & choose Properties)
  4. Open tab "Advanced System Settings".
  5. Opend tab "Advanced"
  6. Click on button "Environment variables".
  7. Find variable CLASSPATH on "Environment variables" area, match it with left button and click "Edit" button.
  8. Paste/type path to your log4j library - LOG4J_ISTALLATION_DIR\log4j-VERSION.jar; (e.g. c:\javalibs\log4j-1.2.16.jar;)
  9. Click "OK" button.
  10. Click "OK" button on "Environment variables" window.
  11. Click "OK" button on "System Properties" window.
  12. Go to command line and check if everything works by trying example from log4j short tutorial (attachede below):

    cd c:\Users\User\log4j
    javac MyApp2.java
    2012-03-30 21:32 510 Bar.class
    2012-03-30 21:24 177 Bar.java
    2012-03-30 21:26 334 MyApp2-log4j.properties
    2012-03-30 21:32 916 MyApp2.class
    2012-03-30 21:25 775 MyApp2.java
    java MyApp2 MyApp2-log4j.properties
    0 [main] INFO MyApp2 - Entering application
    2 [main] DEBUG Bar - Did it again
    3 [main] INFO MyApp2 - Exiting application

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

import org.apache.log4j.Logger;

public class Bar {
  static Logger logger = Logger.getLogger(Bar.class);
  public void doIt() {
    logger.debug("Did it again!");
  }
}

// 3. Create below MyApp2 class and save as MyApp2.java

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class MyApp2 {

  static Logger logger = Logger.getLogger(MyApp.class.getName());

  public static void main(String[] args) {

    // BasicConfigurator replaced with PropertyConfigurator.
    PropertyConfigurator.configure(args[0]);

    logger.info("Entering application.");
    Bar bar = new Bar();
    bar.doIt();
    logger.info("Exiting application.");
  }
}

// 4. Create & save MyApp2-log4j.properties file which contains below code:

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

// 5. Try to compile above example, it should return something like this:

> c:\Users\Hopbit\log4j>javac MyApp2.java
> MyApp2.java:1: package org.apache.log4j does not exist
> import org.apache.log4j.Logger;
>                         ^
> // [...] SOME OTHER ERRORS HERE

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.

查看更多
登录 后发表回答