Installing a new API in Java for OS X - java

I've downloaded a new api for Java that accesses excel files, but am unsure on how to install it so that it can be imported for use in my program. Help is appreciated. Thanks

To the point: just put it in the classpath.
A classpath is basically a collection of disk file system paths to a root folder where all classes are located like /path/to/package/root and/or paths to the JAR file itself like /path/to/file.jar. You can specify multiple paths in the classpath by a separator character. In Unix based systems like OS X the separator character is the colon : (on Windows it's the semicolon ;).
How and where to specify the classpath depends on how you're compiling and executing the program.
If you're using plain javac to compile the program, then use the -cp argument to specify the compile time classpath. Or if you're using an IDE, then add it to the project's Build Path (which covers both the compile time and runtime classpath).
If you're using java to execute the program as a simple .class file, then use the -cp argument the same way. If you're using java -jar (or doubleclicking the file in some platform specific UI explorer) to execute the program as an executabele .jar file, then you need to specify it in Class-Path entry of the JAR's MANIFEST.MF file. This one can be relative to the JAR file's location.

You don't really have to "install" it - you just have to put it inside the Classpath. For example, if you're using Eclipse, you can right-click on your project, select something like "build path"->"configure build path", then libraries.

That depends on the tools you are using for development. Basically it will have to be included on your classpath for your IDE project for development, and in your runtime classpath at deployment time.
How to accomplish this in development is specific to your project configuration, IDE and how you store dependent jar files in your development environment (i.e. shared lib directory, maven, project lib folder ...).

Related

Eclipse Java project: can I set eclipse to store .class and .java files in 1 folder?

I just need a simple environment of .class and .java files in a single folder so that I can execute java files later on using "java xx" in command line without adding any extra syntax. I'm not planning to use packages or sub-directories in my project and I won't be executing any files directly from eclipse either.
You could create a executable jar file and run it from command line using:
java -jar MyApplication.jar
Here is the link to create an executable jar file from eclipse:
How to create an Executable jar file
I think you are looking for this:
Create new project->Java Project->In the Project Layout section change to 'User project folder as root for sources and class files' (using Eclipse Kepler, it should be similar in other versions of Eclipse).
But keep in mind that it might become a mess after some time.

How to set classpath from a config file for a Java application

Hi, I am developing an application in Java. I have to run that jar in different Operating Systems, such as winxp, linux, rhel, Ubuntu.
I have some jar files that must be in the classpath.
The client is not ready to set the path, so I have to set it programmatically.
My problem is with the different Operating Systems, because the user will have different paths. How can I get the path through a config file?
How do I manage OS and these dependencies?
Any idea if I can set the path at runtime?
You should package all the jars your program depends inside your executable jar. In your project create lib directory and push all the dependencies inside. This will save you lot of pain.
If you use Maven2 , you can do something like this in your pom.xml.
In case you build from IDE (Eclipse or IntelliJ etc) you can use the wizard: R.click your project root -> Export -> Runnable JAR etc .

Where does classpath ends and starts?

What is meant by saying project is on the classpath? For example, I keep a file within project directory (Project/file), is that on the classpath? How should I know what the classpath involves in my project?
I am trying to read text file by using InputStream object. The idea is to read the file whilst you are running the jar file (the app). As far as I know in case of InputStream object the file shall be on the classpath. So, where does the file supposed to be? I am developing on Eclipse IDE.
Thanks
The project's Java Build Path determines what your project's sources consider to be on their CLASSPATH, as well as what's used when launching applications in that project. If you're running a .jar externally, it's what you've set in that environmental variable or with the -classpath/-cp arguments.
Classpath during eclipse development is determined by Configure Build Path dialog. Classpath during runtime is determined by runtime conditions. In eclipse it is done by Run (or Debug) configurations dialog. If you deploy your application somewhere, then classpath is determined by conditions at deployment place.

What is the difference between setting the classpath and java build path in eclipse?

What are the different ways that java programs gain access to external libraries. There is setting a classpath, modifying the build or build path, but I've seen other ways of adding jars.
Why do some libraries have to be added to the classpath while others do not. For example I'm using JSF, WTP tools, and other extra libraries but they are not in my buildpath when I view the build path of my project.
The classpath is used to find classes when executing a Java program. The build path is used when Eclipse is compiling a Java program.
The Java Build Path is just an Eclipse thing. It's where Eclipse finds the classes needed to compile and run the classes of the project. It's thus both th compile and the run classpath.
In the case of a webapp, the webapp runs inside a Java EE web container. The web container gives access to standard Java EE classes (javax.servlet, etc.). Moreover, all the jars in WEB-INF/classes are automatically included in the classpath of the web app. So Eclipse doesn't need you to specify them in the Java Build Path. They're included automatically.
On development time.
A build path is one where you can explicitly point to third party software / jars.
By default not all third party software are added into your classpath, hence you may have to explicitly add that to your path.
On runtime.
On the other hand when you run your applications from the command line, you would prefix the classpath by using -cp to specify the third party jars.
For example in web projects you would add it to your web-inf library when you deploy.
A classpath is simply an array of classpath entries (IClasspathEntry) that describe the types that are available. The classpath is an environment variable that tells where to look for class files and it is generally set to a directory or a JAR (java archive) file.
The Java build path is reflected in the structure of a Java project element. You can query a project for its package fragment roots (IPackageFragmentRoot). The build path is the classpath that is used for building a Java project (IJavaProject).

What is the difference between Class Path and Build Path

I'm confused with these two terms.
Also what should I do to create a file under the src folder of a Spring MVC Project?
When I create using a File object it creates the file inside C:\SpringSourceTool...
I guess this is ClassPath right?
How can I get the applicationcontext folder or root of the application whatever?
The build path is used for building your application. It contains all of your source files and all Java libraries that are required to compile the application.
The classpath is used for executing the application. This includes all java classes and libraries that are needed to run the java application. A Classpath is mandatory, the default path is . which is used if the java virtual machine can't find a user defined path. (CLASSPATH environment variable, -cp flag or Class-Path: attribute in a jar manifest)
The classpath is the conventional way to tell the (standard) Java compiler and the Java runtime where to find compiled classes. It is typically a sequence of JAR file names and directory names. The classpath used by the compiler and the runtime system don't have to be the same, but they typically should be, especially for a small project.
Buildpath is not standard Java terminology. It is the term for the richer way that a typical IDE specifies the relationship between the "projects" that make up an application. The IDE uses this to figure out the classpath and sourcepath for compiling the Java code, and the classpath for running it. The IDE also uses the build path to figure out how to package up your code and its dependencies as (for example) a WAR file.
For example, an Eclipse build path for a project includes the other projects that it depends on, and lists any additional library JARs that the project contains / relies on. It also lists the packages in the current project that downstream projects can depend on.
(If you are using Maven for your project, the IDE buildpath mechanism is secondary to the dependencies declared in the POM files. For example, using Eclipse with the m2eclipse, the buildpath is synthesized from the POM files.)
The class path is used at runtime to load compiled classes and resources.
The build path is used at compile time to find the dependencies needed to build your project.
I would like to add to Andreas_D's answer to explain that the build path is required by the IDE/compiler to locate external packages and classes used by your code. We sometimes refer to these as 'dependencies'.
NB: These external packages may be packaged inside a compressed .jar file or indeed, there may be several jar files packaged inside a 'library'. A library or group of libraries often make up a 'framework'.
If your code requires code written by others, you can import them into your class using the import command. However, this command on its own is insufficient as the compiler or IDE needs to know where those classes are located. You specify this in the build path.
The classpath on the other hand tells the JVM running your application where to find any dependencies during the actual execution of your code.
Also to note:
Classpath is for use by the JVM.
Buildpath is for use by the IDE/compiler and is a means to construct the classpath from your development environment. When you configure your buildpath via your IDE, you are also configuring a hidden file in your project called .classpath. This is used to provide the classpath to JVM at deployment.
Each Java project has its own build path that specifies all dependencies required to compile the project. Those dependencies may come from other Java projects in the workspace, from Java archive .jar files, or from folders containing .class files.
In CLASSPATH environment you need to specify only .class files (i.e., jar, zip files – Inside jar, zip files you will find only java classes) i.e. you are helping Java Virtual Machine (JVM) to find Java class files
Also what should i do to create a file
under the src folder of a Spring MVC
Project? When i create using a File
object it creates the file inside
C:\SpringSourceTool...
This is where the JVM was started, if you want to create the file else where, use relative path from here.
See this and this for more info.
Classpath (from Wikipedia):
Similar to the classic dynamic loading behavior, when executing Java
programs, the Java Virtual Machine finds and loads classes lazily (it
loads the bytecode of a class only when the class is first used). The
classpath tells Java where to look in the filesystem for files
defining these classes.
The virtual machine searches for and loads classes in this order:
bootstrap classes: the classes that are fundamental to the Java
Platform (comprising the public classes of the Java Class Library, and
the private classes that are necessary for this library to be
functional).
extension classes: packages that are in the extension
directory of the JRE or JDK,
jre/lib/ext/ user-defined packages and
libraries
By default only the packages of the JDK standard API and
extension packages are accessible without needing to set where to find
them. The path for all user-defined packages and libraries must be set
in the command-line (or in the Manifest associated with the Jar file
containing the classes).
Simply put - while your program is running, the JVM loads classes only as needed. When a class is needed, the JVM will depend on the classpath to it know where to load the bytecode from (i.e.: .class files).
Build path, on the other hand, is typically used by an IDE, such as Eclipse, to know where to look for additional libraries that are required to compile a project's source code. Build path isn't used during runtime.

Categories