not setting CLASSPATH in bash - java

So basically i have a project with a lot of packages and the corresponding jars.
im trying to run the program and to do so i need to set my CLASSPATH the thing is when im trying to set it it says command not found for some reason.
What i did was:
martim#localhost:~/project/woo-app>CLASSPATH = /usr/share/java/po-uulib.jar:/home/martimc/project/woo-app/:/home/martimc/project/woo-app/
Bash : CLASSPATH: command not found
I dont understand why this is happening

Setting environment variables in bash requires no spaces before or after the =. Furthermore, you'd have to put export in front or it won't actually provide that var to java. Lastly, don't use CLASSPATH at all; java can run many apps, apps need different classpath, so the concept of a global classpath makes no sense. Just use the -cp parameter.

Related

Have a difficulty with installing a .jar library

I have just downloaded a third party java library which i need for a program i'm about to create.
But i can't figure out how to actually install the library so that i can literally type
import path.to.library;
in my java class file without having any errors.
I have looked at many tutorials and answers on StackOverflow but each of them seems to include the use of some or the other IDEs for java.
Well, i'm a bit rustic and would like to know how to make it work with notepad and the command line, coz that's what i use to make a program.
When you are compiling, include the following in your line:
-classpath nameOfJar.jar
However, once you actually switch to use an IDE, you will see the multiple benefits this approach can bring.
You don't specify a path in the import statement, just the package name.
All usable JAR files have to be specified in the classpath on commandline when starting your Java program.
You need to understand how Java's classpath works. For a comprehensive description, read the Oracle manual page on this topic. Alternatively the PATH and CLASSPATH page of the Java Tutorial.
(FWIW - it is generally considered to be a bad idea to use the CLASSPATH environment variable to set the classpath, because this is liable to lead to "nasty surprises" if you deal with software that requires different classpaths.)
If you don't use any IDE you won't have code complete. However if you know all the packages/classes/methods names/signitures you can use pure Notepad and then compile it by adding the library to your classpath (eg. using the -cp switch in the javac command when compiling)
JARs are not required to be installed. They required to be accessible at compile- and run- time. You can add jar by command line parameter or CLASSPATH environment variable. IDEs have special means for setting JAR;s location in visual manner.
You'll need to be more specific about what you have tried so far. This generally isn't something complex though, if you want to manually invoke the compiler you would do something like
javac -cp somejar.jar myclass
Once you get used to this process, it's better to automate it using a build tool such as ant or maven. Ant is a little easier to begin with, maven has some additional capabilities that make it a little more complex.

Compiling Java package throws errors for external Jars

Pretty basic problem here. So I have a Java package that I have created that has three classes (one has the main method). I am trying to use a few Apache Jars, and have added these to my build path in Eclipse. However Eclipse wont let me build and run it properly, so I am trying the command line. I have added the env var CLASSPATH and pointed it to my lib directory which hold the Apache Jars. However, when I try to use javac I get a bunch of errors:
package org.apache.xmlrpc does not exist
import org.apache.xmlrpc.client.XmlRpcClient;
I was reading the man page for javac and it said that:
If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.
So I tried copying the Jars to the same location as my three source files, but no change.
Can someone please tell me what I'm doing wrong?
Thanks.
Classpath variable (or command line option of javac) must contain all jars explicitly. It cannot go through jar files stored in specified directory.
You can compile this by specifying the option -cp on the command line:
javac -cp foo.jar:bar.jar foo/bar/Baz.java
You then run it with the same option:
java -cp foo.jar:bar.jar foo.bar.Baz
It sounds like you've just set the classpath to the directory containing the jar files. You need to set it to the individual jar files, or use java.ext.dirs to set an "extension" directory containing jar files. I'd recommend using the specific jar files. Something like:
// Assuming Windows...
CLASSPATH = c:\libs\foo.jar;c:\libs\bar.jar
I'd also personally recommend specifying the classpath on the command line instead of using an environment variable - the latter will work, but it ends up being a bit more fiddly if you want to compile different projects against different libraries.
However, I'd actually recommend getting Eclipse working first, rather than retreating to the command line. It should be fine - if you could give us more information about what's failing in Eclipse, we may be able to help you with that instead.
The jar files in the current directory are not automatically included; that only refers to .class files in normal package/directory hierarchy. Jar files must be added either explicitly, or via a wildcard like javac -cp ./* (Assuming JDK6+)
(Some OSes may require an escape of the * to avoid globbing; OSX does not.)
I agree with previous answers, but I would also recommend to use proper java build tool - like ant (perceived easier to use, but not necessary) or maven ( perceived more difficult to use, but really worth learning )

java: importing, class path, and packages

I have a file which imports org.w3c.dom.Document. Compiling and running is fine, but I don't understand how it knows where to find this package and I'm just curious how it works. I used the locate command to try and find org.w3c.dom but I get nothing. Where are these packages located? It seems to me that the right place to look would the CLASSPATH environment variable since my search results seem to be suggesting that. Is this correct? In any case, I don't know how to find out what my CLASSPATH variable is. It doesn't seem to be an environment variable that my shell knows about.
That would be part of the core libraries (rt.jar), so it'd be wherever you installed the java JRE; specifically under $JAVA_HOME/jre/lib
You can look inside the .jar files using the jar command. To see the class you mention, you can do:
jar tvf rt.jar
This lists all the classes in that jar.
Note that this location is automatically searched by the JVM - it's not needed nor included in the CLASS_PATH environment variable. (You could add it, but it would simply be redundant)
Edit for clarity:
The JVM includes <Where_you_installed_jdk>/jre/lib and <Where_you_installed_jdk>/jre/lib/ext by default. Anything else has to be explicitly added by you via either passing it to java directly via the -cp option or adding it to the CLASS_PATH environment variable.
The relavent documentation can be found at: http://download.oracle.com/javase/6/docs/technotes/tools/findingclasses.html
The JVM finds classes using classpath settings where alll paths to required packages are set. The classpath could be set with a number of ways. The first mentioned by you is CLASSPATH environment variable. It is optional and can be unset. The second way is an explicit option "-cp" for "java" executable.
Also some JRE runtime jars are added to classpath by default implicitly so you don't need to search and add standard packages by yourself (particulary the one you mentioned in your question).
try compiling messconvener.java like this from its own directory
javac -d ..\..\. -cp ..\..\. messconvener.java
-d - creates directory structure for your package
-cp - provides class path for user file, where it can find user defined classes

no output when .jar is executed

I built an application in Netbeans 6.8 and made project.jar file. When I run it, it works only on my computer, but not on any other computer. However, when I made any simple application, that doesnt use any libraries, it works fine on any computer.
Is there any way, how to invoke some error message, where is the problem?
My project use R 2.9.2, so I install this version on other computer and set the System Path variable exactly same. Other libraries listed in lib directory are: AbsoluteLayout.jar,DatePicker-V0.99-2006.09.01.jar,jcommon-1.0.16.jar,jfreechart-1.0.13.jar,jmathplot.jar,JRI.jar,pdf-renderer-1.0.5.jar
Thank you
You don't get any message at all? What do "works" and "not works" look like?
You sound like another person who hasn't taken the time to learn how to do things by hand on the command line without an IDE. I'd recommend doing that. Open a command shell and type in the java -jar -cp ... foo.jar command to run your stuff. The messages you get back will be educational.
Note the -cp command line argument. That's how you add your JARs to the CLASSPATH properly.
I solved this problem as follows, maybe it will help someone.I add 2 paths in PATH system variable:
Start -> Control Panel -> System -> Advanced
Click on Environment Variables, under System Variables, find PATH, and click on it.
In the Edit windows, modify PATH by adding the location of the class to the value for PATH.
you must add both paths, to jri.dll and r.dll, in my case it were these:
C:/Program Files/R/R-2.9.2/bin/;C:/Program Files/R/R-2.9.2/library/rJava/jri/;
I have added these lines already, but with different different slash. So be careful, you must use it / not \ to define path!!!

Can you set the current running directory from the java command line?

I am running a Java application from the command line. Can I specify a command line argument to set the current running directory to something other than where the application is actually going to run?
There is a JVM argument -Duser.dir which can be used to set working directory for JVM.
If it all possible I would rather use a script to run the java application and set the directory in the script:
#!/bin/sh
cd <your dir>
java <some arguments>
The JNI-solution may affect all kinds of relative paths in your application; for examples the classpath you put in.
If you want to change the current directory, you'll have to use JNI and invoke a native API from your Java code. For example, for Windows you would use SetCurrentDirectory
I found this SO post and it helped me solve my problem. Though I wanted to share something specific about IntelliJ that might trip somebody else up.
I have posted a picture below where the -Duser.dir flag is used and also the Working Directory text field is filled in.
In this situation, the Working Directory will be set to "JarLearning" rather than "2ndTry".

Categories