• Post Reply Bookmark Topic Watch Topic
  • New Topic

import libraries  RSS feed

 
Nandita Tiwari
Ranch Hand
Posts: 77
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to add two libraries to classpath. one is zip file and another is jar file. how m i supposed to do it? java5 and im not using any IDE.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noopur Kore wrote:i want to add two libraries to classpath. one is zip file and another is jar file. how m i supposed to do it?

-cp classpath
Specify a list of directories, JAR archives, and ZIP archives to search for class files. Class path entries are separated by semicolons (;). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the current directory (.).
hope this helps
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noopur Kore wrote:i want to add two libraries to classpath. one is zip file and another is jar file. how m i supposed to do it? java5 and im not using any IDE.

While running your code (by using 'java' command), you can provide those libraries by either -cp or -classpath command line argument.
You can also check the help message of java executable.

On a side-note, any specific reason for using a Java version which is deprecated long time back(Java 5)?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naved momin wrote:Class path entries are separated by semicolons (;).

On Windows. On *nix based systems it's a colon (:)
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just want to add what naved said, if you specify -cp or -classpath switch it will override any values you set either through environement variable or through command prompt. also it will override the default value of classpth which is current directory. so if you want to include the current directory in your classpath then you should include the current directory in the classpath . here is a sample for you

java -cp c:/Projects;d:/test;d:/test/My.jar HelloWorld



Also keep in mind that if you have organized your classes into packages, then the classpath should contain the directory WHICH IS THE PARENT OF THE PACKAGE ROOT. for e.g suppose you have a classfile like com.aladeen.Test . also consider the following file system hierarchy Guru>com>aladeen>Test. then you have to include the Guru directory in your classpath, since Guru directory is the parent of the package root(i.e com). i know it is confusing. if you get it,well and good , if not then post it, will post in a much better way.
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:java -cp c:/Projects;d:/test;d:/test/My.jar HelloWorld

On Windows you really need to use \ in paths. Java code (with java.io.File) is lenient enough to let you use /, but the command line definitely isn't. So: java -cp c:\Projects;d:\test;d:\test\My.jar HelloWorld.
 
Nandita Tiwari
Ranch Hand
Posts: 77
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where should i giive the -cp or -classpath command??
java 5 becoz im using oracle 10g and it library supports java 1.5.. so i thot i better use older version inspite of having the new one..
 
fred rosenberger
lowercase baba
Bartender
Posts: 12559
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noopur Kore wrote:where should i giive the -cp or -classpath command??

Did you read Rob's post? He very clearly shows you where to put it.

Not that it is not a COMMAND, but an argument/flag given to the java program.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
On Windows you really need to use \ in paths. Java code (with java.io.File) is lenient enough to let you use /, but the command line definitely isn't.


Isn't it definitely? Windows XP's cmd.exe seems to handle them the same way. The invoked class is found with both was of setting the class path:

>java15 -cp build\WEB-INF\classes hu.khb.szep.common.CommonTool
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at hu.khb.szep.common.CommonTool.<clinit>(CommonTool.java:11)

>java15 -cp build/WEB-INF/classes hu.khb.szep.common.CommonTool
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at hu.khb.szep.common.CommonTool.<clinit>(CommonTool.java:11)
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!