0

So I am not familiar with adding new libraries to java. I downloaded a library from GitHub and I got a zip file.

I have extracted the contents but I'm lost. I dont know where and how I should add this library.

I dont use any IDE. I use the command prompt on windows 8. I use notepad to type my code

I have done my research for the last 2 hours but all of them are for some IDE and the ones that I found other than those didnt help.

I read about -classpath but I'll have to set the path every time I have a new library?? (I'm still confused)

Please tell me how to add this library. Thank you!

  • Simple: use an IDE. – Nabin Bhandari Oct 06 '17 at 02:20
  • 1
    Have a look here : https://stackoverflow.com/questions/9395207/how-to-include-jar-files-with-java-file-and-compile-in-command-prompt And of course start using an IDE ( Eclipse , Intelij etc ) it will increase your productivity and make your life easier ;) – JKostikiadis Oct 06 '17 at 02:20
  • @EdwinDalorzo Yes I have installed maven. I have got 3 jar files and a bunch of other files with it when I ran "maven clean package" So If I copy those jar files to lib folder i'm done? – CodeBloodedProgrammer Oct 06 '17 at 02:29
  • @EdwinDalorzo Its very confusing :/ I ran "maven clean package" on the folder that I extracted from that zip file...Am I doing it wrong? Should I create my java program and then run the command on that ?? – CodeBloodedProgrammer Oct 06 '17 at 02:36
  • @MrX: to use a jar with a maven project, add the jar as a dependency in your pom.xml. There is no lib folder. And notepad is about the worst editor ever. – Nathan Hughes Oct 06 '17 at 03:36
  • @NathanHughes I know as people stick to easy access ready baked 1 click codes through IDE's, I prefer notepad over them so I have to type everything I'm doing and when I start to debug, Hell breaks loose when there's 500+ lines of code. But in the process I think like a machine. It definitely helps me as I'm learning java myself. – CodeBloodedProgrammer Oct 06 '17 at 04:39
  • @MrX I think your approach is the right one. If you're relative new to Java, using an IDE is a good way to ensure that you _never_ really understand why you have the problems you face, and if you're ever faced with a problem your IDE can't solve, you'll be stuck. IDEs have their place, of course, but as learning tools they're terrible. – Kevin Boone Oct 06 '17 at 08:59
  • @KevinBoone Thanks man :D – CodeBloodedProgrammer Oct 08 '17 at 04:55

3 Answers3

1

Suppose I have two Java libraries packaged as jar files: foo.jar and bar.jar

The foo.jar contains the class Foo.class and the bar.jar contains the class Bar.class.

Now I am writing a piece of code in which I use both Foo and Bar.

package my.work;

public class TestDrive {
   public static void main(String[] args) {
      Foo foo = new Foo();
      Bar bar = new Bar();
      System.out.println("Hello, " + foo + " and " + bar);
   }
}

In order to compile TestDrive.java I need to know where both dependencies are located. Most likely I put them in my libs folder

+-project
|   +---src
|   |    +--TestDrive.java
|   +---libs
|   |    +--foo.jar
|   |    +--bar.jar
|   +---build
|   |    + my
|   |    |   +-- work 
|   |    |   |    +--TestDrive.class //once I compile it with javac

Then I would need to do something like

javac -classpath libs/foo.jar:libs/bar.jar -d build -sourcepath src src/TestDrive.java

So, to answer your question, yes, you need to put all your libraries in the classpath.

There are, of course, other ways to do this.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
  • Thank you! You gave me a clear picture on the problem. I guess I'll have to take the long way if I am sticking to the command prompt. – CodeBloodedProgrammer Oct 06 '17 at 02:56
  • This is the standard way to run java applications. Even IDE is doing the same thing from behind. If you don't want to type so long for your compilation and execution, try using bat files. – Alex Oct 06 '17 at 03:00
  • @Edwin Dalorzo I'm stuck bud. I compiled my one liner but I am unable to run the program with -cp. It gives me an error saying it cant find or load the class file. – CodeBloodedProgrammer Oct 08 '17 at 04:58
0

I read about -classpath but I'll have to set the path every time I have a new library?? (I'm still confused)

No.
You have to set it every time you run your program!

So you either create a script to run your program, or set it thru the MANIFEST.MF file of your program's main JAR...

See:

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
0

Set an Environment variable CLASSPATH in System Properties--> Advanced --> Environment Variables

For example if you have multiple jar files, copy them to a dir in C:\LIBS\ and set classpath value as C:\LIBS\*;.

If you want for specific jar files only set it as, C:\LIBS\jar1.jar;C:\LIBS\jar2.jar;.

Open new command prompt and it should work and is permanent

NOTE : This impacts all the instances of command prompts opened further

Harshavardhan Konakanchi
  • 4,238
  • 6
  • 36
  • 54
  • "This impacts all the instances of command prompts opened further" This is what I was concerned about. If I do this, will the way I run and compile all the programs change ? – CodeBloodedProgrammer Oct 06 '17 at 02:41
  • This CLASSPATH will be used by all the programs i.e impacted by javac and java commands when used in command line – Harshavardhan Konakanchi Oct 06 '17 at 02:43
  • "To run it successfully you need to go back to directory C:\project and now run C:\project>java testing.Test" This I found here about classpath http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html#ixzz4ugzTNDNi So I was thinking it would change the way i run programs and didnt wanna complicate things. I simply do " java program.java" and javac program. – CodeBloodedProgrammer Oct 06 '17 at 02:50
  • The . ( dot or period ) at the end of classpath specifies, current working directory and you can give your build output dir like C:\Project replacing the . (dot) – Harshavardhan Konakanchi Oct 06 '17 at 02:53
  • Hmmm I get it! Thanks for helping! :D I have to do more research on this now. – CodeBloodedProgrammer Oct 06 '17 at 02:59