How to include libraries in Java without using an IDE - java

How do I import libraries in my Java program without using an IDE, like NetBeans?
In NetBeans I do it this way:
How can I achieve the same thing by just using Notepad++ or Programmer's Notepad.
As much as possible I don't want to use NetBeans because it would be overkill since I'm only working on simple projects.

Use:
javac -classpath external.jar myClass.java
If your main class is in a package,
package com.mycompany;
public class myClass
{
...
...
then you'll need
javac -classpath external.jar com/mycompany/myClass.java
And to run:
java -classpath external.jar com.mycompany.myClass

In addition to Bala R's post, adding multiple files and locations is perfectly OK too...
javac -cp location1/;location2/;file1.jar;file2.jar fileToCompile
Notes:
-cp and -classpath are the same thing.
If you're on Solaris (and some other Unix flavors), change the ';' to ':'.

All of the other posters are spot on; you just need to add the JAR file to your classpath.
Java offers many mechanisms for setting the classpath, including via the command line, via an environment variable, and through setting it in the MANIFEST.MF of an executable Java JAR file.
These are all a pain in the neck to manage. It's good to know the technique, and understand the basics. But it's really a bad idea to actually use them.
You should do this.
First, put all of your Java libraries in a single place on your system. C:\java\libraries, or whatever. Someplace that you remember, and someplace accessible by all of your projects.
Next, name all of your libraries using their version numbers. If you using Log4j v1.4.1, then put the JAR file in a log4j-1.4.1 directory in your library area. This gives you "free" library versioning.
Finally, learn Ant. For simple projects, Ant is simple. Use the Ant build.xml file to compile, test, and run your application.
Why? Several reasons.
Because once it's set up, adding a new library to your project is trivial; you add a line to your build.xml file. Ant lets you more easily handle simple abstractions (like where all of your libraries are located).
The build.xml file is self-contained. If you use, say, an environment variable for the classpath, then the classpath for one project may be different from that of another. That means resetting the environment variable. Continue this and you'll end up swearing at some "new problem" where it "worked before" when it's because you had your classpath set wrong. Set it once in the build.xml file, and forget it.
Ant is portable. It runs the same on Windows, on Linux, on Mac, on AS/400, it runs everywhere that Java runs, unlike shells scripts or BAT files.
It's lightweight. Simple Ant scripts are simple. They don't bring a lot of baggage with them, and you can always make them scary complicated. It's much simpler than Maven for just builds.
Most IDEs support Ant directly. If you decided to go back to an IDE, most can simply use your Ant build file with minimal configuration.
This is how you solve your classpath problem with Notepad++. Setting the classpath works, but it doesn't go far enough. It's a pain to administer and manage. Learning the basics of Ant will take you much farther with minimal work.

You should put them on your classpath, like
java -classpath someJar.jar YourMainClass
And, of course, you can do the same for javac.
If you need to have more than one JAR file or directory on your classpath, you'll need to use your platform's default path separator. For example, on Windows,
java -classpath someJar.jar;myJar.jar YourMainClass
On a side note, you might find it easier to use an IDE to manage this sort of stuff. I've personally used just my slightly scriptable editor and have managed fine. But it's good to know how to do this stuff by the command line.

Put the JAR files in your classpath. classpath is an environment variable.

Make sure the JAR file is in your classpath and you have the import statement.

Related

Set CLASSPATH on Mac

Can someone please help me, I'm almost desperate because I can't find solutions for a Mac.
I have written two .java files that I would like to use as a library for all new projects.
For this I found out that I have to set the CLASSPATH.
The java files path:
/Users/thomas/Soft/IN_OUT/In.java
/Users/thomas/Soft/IN_OUT/Out.java
Don't bother with it. The concept of CLASSPATH is misguided in the first place (because you can run more than one java app, ever, on your system, setting the classpath globally is misguided as a concept), and on most platforms, libraries and tools, this isn't the intended way to configure classpaths.
For example, in IDEs, you configure classpaths on a per project basis.
When compiling code or with javac or running code with java, you can supply the classpath with the -classpath option (or shorten it to -cp). For running jar files (java -jar foo.jar, or double clicking a jar), the classpath is taken solely from the Class-Path manifest entry in that jar file; both the -cp and the global CLASSPATH variable are ignored.
I get that you want these files to be available to 'all projects', but that's just not how it works. If you update these files, all your old projects would break, or at least you'd have to consider if they might. You'd also be extending these files to eclipse, intellij, and any other java apps on your system which makes no sense. That's why this notion of 'make these available to everything' isn't a particularly sensible thing to want.

Where can I put my java libraries so they will always be in the class path when using javac?

I'm using a mac, and I would like to know if there is a directory in which I can put all my java libraries that I use often so that I can compile/run the code that uses them without explicitly setting the classpath each time? I want to do something analogous to what a package manager would do in python, but it doesn't seem like there are any package managers for java.
I'd strongly advise that you avoid doing this. While it might be convenient for you personally, it's terrible from a repeatability standpoint. If you have trouble with your code and want to send your project to somebody else to look at, they shouldn't have to reproduce your entire environment to be able to compile it.
For package management in Java, look at the Maven project. This will allow you to describe your project dependencies and have the tool automatically download the appropriate JARs and add them to your project's classpath.
You can put them anywhere you like, and set the CLASSPATH environment variable so that it includes that path. javac uses the environment variable, so you don't have to specify it each time.
As you're using a Mac, I'll point you to this SO question and its answers about how to set environment variables. To include multiple directories in your CLASSPATH, separate them with the standard path separator (which I believe is : on Mac OS X).
Two options:
1 - set a CLASSPATH in your ~/.profile file. Then every time you open a Terminal, it will be there.
2 - You can also put the JAR files into the lib/ext directory for your JDK. See this article for the details on that although note that Apple recommends you not do this.

Compiling java from the command line

I am trying to compile an application from the command line
The programme compiles and runs fine in eclipse, howvever when i goto the command line and use javac to compile my java file i get 23 errors, the majority of which are Cannot Find Symbol, with an arrow pointing to the . in a package name.
Does anyone have any ideas on what i need to do differently?
Thanks
Your classpath is not set up correctly. Look at your Eclipse project in the .classpath file. In there you will find a lot of classpathentry elements. You will need to replicate this for your command line compilation.
To do this manually you must first set your CLASSPATH environment variable to a list of directories (or jar files) containing class definitions.
You can also use a build tool called ant to automate this for you.
I advise against setting the classpath as an environment variable because it is too intrusive (all your java programs will see it).
A command line for compiling a Java app which dependes on Log4j might look like this:
javac -cp C:\dev\mvn\repo\log4j\log4j\1.2.16\log4j-1.2.16.jar AppenderTester.java
If you have multiple classpath entries you need to separate them with a semicolon.
For ease of use you could create a startup script. This can be a simple batch script or a more elaborate ant script (which requires installing ant).
This is only the tip of the iceberg known as 'classpath hell'.
EDIT: you can also take a look at the Eclipse feature 'export runnable JAR', which packs your application together with all its dependencies in a JAR file.

Where do I put Java Dependencies to run crawler4j-2.2.jar?

I'm new to Java and aren't sure where to place the Java Dependencies which are required to run crawler4j. Do I put them in the same folder, or do I put them where Java is located on my machine, or what? Please help me.
Putting the dependent JARs in the same folder as your application JAR / bytecode files is a reasonable approach. As others mention, you need to ensure that the actual folder containing the JARs is on the classpath when the JVM is launched to run the application. The -cp argument is the recommended way to do this, and it is common practice to create a little shell script / batch file to launch the app with the appropriate JVM parameters.
Putting them into the Java installation is not a good idea for a couple of reasons.
It might have unforeseen side-effects on other applications run using that installation. This includes applications run by other users.
It will make upgrading your Java installation to the next patch level more difficult.
You need to put them on the CLASSPATH. If you're running your/the application from the command line you can specify your classpath using the -cp argument for java
You put them in your classpath. The classpath can be specified with the -cp argument when you run the java program.
java -cp depend1.jar;depend2.jar;etc... Class2Run

How do you use a Java Library?

I'm trying to use an open source java library to visualize nodes and edges in a graph, but I'm completely lost.
I have a bunch of jar files in a folder. Clicking on some of the jar files makes java swing windows pop open with graphs displayed. Clicking other jar files does nothing.
If I figured that out, would I just stick the jar files in there with the other ones, or would that still not work?
And if I ever figure out how to use these files, does that mean that I have to include them if I transfer my java project to another computer? How would I go about doing that?
I believe if you put the jars in your classpath, you can import and use classes just like you would a standard library. Figuring out the classpath can be confusing, but you can just set it when you start your jvm. Your IDE may have options for it, too.
Most java problems are classpath problems.
Have you included those libraries in your classpath?
If you are using eclipse, you could
Project - > properties -> Java build path ->addJar.
And the Jar file should be placed in a directory inside your workspace (lib/ for example)
If you have to take your project to another computer, you could take these steps
Before doing anything, export your project (as a Jar file, for example).
Save it into your favorite drive (cd / usb drive/ diskette/ tape).
On "the other" computer, you can import this project into your workspace
In Eclipse, you need to add libraries to the project build path.
In general, you need to provide dependencies via the classpath mechanisms at compile time and runtime. The precise mechanisms vary, but, for example, if you used the javac compiler, you would provide your libraries on the command line:
javac -classpath C:\dir\lib1.jar;C:\dir\lib2.jar foo/MyClass.java
These dependencies would also be required to invoke the app:
java -classpath C:\dir\lib1.jar;C:\dir\lib2.jar;. foo.MyClass
This page gives some good info, though googling for the term "classpath" should provide alternative sources.
You use it by including it in the classpath of your java application, that way you can reference it from your code. Here is a starter document. The JDK 1.6 has some easier options (such as specifying multiple jar files as *.jar). It is definitely a little complicated, but it is very worth knowing.
You should have documentation for these Jars. Some sounds like examples, but one must be the core graph modelling and rendering Jar. Hopefully the examples have source included.
Just add that Jar to your project in Eclipse (e.g., in a /lib folder in your project, then add it to the build path) and use the documentation to use the code. You can also use Eclipse to look inside the Jar file.
Unless there is no alternative, it probably isn't worth using a load of third party code that isn't documented at least on the API level, and without any source examples definitely not.

Categories