Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to access classes in a jar file  RSS feed

 
sylvia greene
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a folder called src. In this folder i have a jar file log4j-1.2.7 which contains packages. In the same folder src i have another package com.abc which has a file App.java
When i compile this class it cannot access classes within the jar file.

structure is something like this

src\log4j-1.2.7.jar

and

src\classes\com\abc\App.java

I am writing
D:\>src\classes>javac com\abc\App.java

It's unable to find the classes in jar file
 
Shashank Agarwal
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please write the error message that you are recieving.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sylvia,

I think your problem may be twofold. Firstly the directory structure of your application; secondly your classpath.

Set up your application directory structure something like the following:

This is the conventional way of setting up a Java app.

Ensure the following directories are on your classpath:

If you're using an IDE then set the classpath in your IDE configuration.
On the command line, you can specify the classpath as follows (ignoring the classes directory):

Or set the environment variable 'classpath' as follows:

Note that setting the classpath environment variable in a command window is only persistent as long as the command window is open (i.e. it doesn't change your system settings)

From the src directory, you should then be able to execute:

If successful the class file will appear in the same directory as App.java.
To create the classes in your myapp\classes directory add "-d D:\java\myapp\classes" to the command line.

Just a note about Log4J: Log4J is a very well designed and easy to use logging API, but are you aware that Java 1.4 has its own logging API, very similar to Log4J, in the java.logging package?

Alternatively, if you don't want to mess around with fancy hierarchical directory structures just stick everything under the D:\src directory and make sure it's on the classpath.

Hope you find this useful.

Jules
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shashank,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!