I'm executing a java application in DOS command window using something like
java -cp abcclient.jar;junit-4.4.jar;myapp.jar MyMainClass
I need to reference many other jars that are found in a specific folder outside my application folder. Is there anyway I could state a folder name in the above command line to let java refer to the necessary jars from that folder.
Thanks
With java6, you can use a wildcard in classpath entries, so:
java -cp "abcclient.jar;junit-4.4.jar;myapp.jar;..\lib\*" MyMainClass
should work
(There's some problems explained here though http://javahowto.blogspot.com/2006/07/jdk-6-supports-in-classpath-but-be.html)
The very simplest way to do it is with the extensions mechanism:
java -Djava.ext.dirs=lib MyMainClass
For javac, the equivalent is the -extdirs flag:
javac -extdirs lib MyMainClass.java
It's not ideal - particularly if you also want to use the normal extensions - but it can be a useful little shortcut in some cases.
Related
I am using java1.6 without using any IDE.Now i want to use java Mail API for my purpose.So, i copied Mail.jar into d:\externaljar folder.
And also i have set the classpath as set classpath=%classpath%;d:\externaljar;
my jdk installation folder is : c:\programfiles\jdk1.6.
But i faced package javax.mail does not exist during compilation.
Please Guide me to get out of this issue?
The jar file itself must be in the classpath, and not just the directory containing it.
And the CLASSPATH environment variable is CLASSPATH, not classpath. My advice would be to never use it, though. Always use the -classpath (or -cp) option with javac or java to pass the classpath.
I prefer the -cp option over the global CLASSPATH environment variable:
java -cp .;d:/externaljar/mail.jar my.application.App
I'd recommend against setting CLASSPATH and instead use the -cp flag:
javac -cp .;d:\externaljar\mail.jar whatever/package/YourClass.java
You may also use wildcarding:
javac -cp .;d:\externaljar\* whatever/package/YourClass.java
Running is the same thing, except you provide the classname with the main method.
java -cp .;d:\externaljar\* whatever.package.YourClass
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 )
I've many jar files to add to my classpath, so everytime I compile my java file I end up with a command like this:
javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. CollectionIndexer.java
I've tried to use:
set CLASSPATH=commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:.
and then:
javac CollectionIndexer.java
but the jar are not added at all: I get error due to the missing jars...
thanks
Try using export CLASSPATH=... instead of set CLASSPATH=...
(I assume you're using a Unix box of some description, given the colons in the classpath.)
The most pain-free way, in my opinion, is to create batch files that contain all your project related jars... one to compile and another to run:-
compile.bat
javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1
run.bat
java -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1
With that, you can do this:-
compile.bat CollectionIndexer.java
run.bat CollectionIndexer
Even better, you can combine them together:-
compilerun.bat
Make sure you append ".java" to javac's %1
javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1.java
java -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1
With that, you can do this:-
compilerun.bat CollectionIndexer
I like this approach much better than setting classpath because I don't need to retype the classpath each time I open the terminal. :)
By the way, it is not very nice to modify the CLASSPATH environment variable specifically for compiling a project, given that after that, all other projects will inherit this change. This of course stands only if you are globally changing it. If you aren't and instead you are planning to write a little script to build you project, why don't you consider using ant? Good luck!
You have set the CLASSPATH, but you didn't put it into the environment. So it's a variable, but not quite an environmental variable.
To promote an in-script / in-session variable to an environmental variable, use the command export like so
export CLASSPATH
This promotes the variable to an environmental variable (which will be accessible to any shell that inherits the environment).
Some systems allow the combining of the set and the export. In such systems, you can combine your set command with the export command like so:
export CLASSPATH=<your value here>
The java command only reads the environmental variable CLASSPATH. It can't look into non-environmental variables as those are not inherited from process to process.
maybe you want to try to use maven for build you application?
It's really easy to setup and it annihilate all problems with dependency management.
also from java 6 you can use wildcards in classpath:
set CLASSPATH=my_libs\*;
Besides exporting your UNIX environment, use absolute paths. For example, the class path entry: commons-digester-2.1/commons-digester-2.1.jar only works if you are in the parent directory of the commons-digester-2.1 installation directory.
On unix, there should be a common location into which you install your packages. Something like /usr/local, /usr/lib, or /usr/local/lib.
I downloaded the Javax.mail package. I have jdk1.6.0_11.
Problem is...I cannot get javac or java to find those classes!
I can get apps to compile using JCreator LE ( by adding the mail jar
to its search list ) but, when I try to run the app in a command window,
it fails.
Can I add these new classes to the rt.jar without
hurting my jdk installation?
I know java has it wired up to look there for classes.
(And, the mail classes are inside a javax package - seems like
they could reasonably be added to the javax folder in rt.jar..
Thanks!
Phil D'
No you can't, nor should you.
Instead, figure out the problem with your classloader (probably paths?). You'll need that for the next library you need to access.
Messing with rt.jar means you can't run on any other JVM.
You should either specify the jar file in your classpath: preferably on the command line with the -cp option, but possibly with the CLASSPATH environment variable.
Alternatively, you can specify its directory in the java.ext.dirs system property. For more details, see the documentation for the extensions mechanism.
You shouldn't be messing around with rt.jar. That's very definitely not the way to make extra jar files available - it's akin to trying to add Microsoft Word to the Windows kernel ;)
Adding things to rt.jar seems like a bad idea, even though its possible and easy to accomplish.
Try compile your application from the command line like this:
javac -cp <path_to_3rd_libs>/jarfile.jar . MainClass.java
If the compiler still complains about the javax.mail package try to unpack/examine the jar file to see that javax.mail package (and its expected content) is there.
(On windows its easy to examine a jar file using 7zip.)
Most definitely no.
If you post the command you are running from the command line we will be able to point you on the right direction, but most likely you are just missing a classpath parameter.
java -classpath /path/to/mail.jar MyClass
You need to understand the CLASSPATH concept which allows you to add individual classes and jar files containing classes to the "universe" of defined classes available for the code you want to compile and/or run. It is similar in idea to the PATH variable in the Windows world.
For the Windows command line this is the documentation:
http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html
The Java Tutorial surprised me by not being well-written for this particular concept:
http://java.sun.com/docs/books/tutorial/essential/environment/paths.html
You most likely need something along the lines:
C:> set CLASSPATH=c:\javamail\first.jar;c:\javamail\second.jar
after which both java and javac should know about these classes
I am working on a .jar file library to implement a bunch of helper classes to interface a PC to a piece of external hardware. I'll also add some simple applications, either command-line or GUI, to handle common tasks using the library.
My question is, is there a recommended way to simplify the command-line instantiation of a JVM in a certain specific way?
e.g. instead of requiring a user to type a cryptic error-prone command like:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar arg1 arg2
instead I do
TurboBlenderApp arg1 arg2
?
I suppose I could use shell scripts (incl. win32 Batch Files... gacckkk), it's just that I'm not good at those + I was wondering if there was a more straightforward platform-independent way of running a Java app from the commandline.
When you use -jar, then-cp (and the CLASSPATH variable) will be ignored
Just provide a executable jar. "java -jar TheApp <whateverargumentsyouwant>" shouldn't be too hard (you can have a Class-Path attribute in your jar files manifest, however).
if it is too hard write a GUI
or provide those shell scripts/batch files. Writing those isn't too hard either.
If you just want to simplify local execution, use a batch file or even just create a custom shortcut. If you want to create a launcher and package the executable jar and required libs for deployment, use something like Launch4j.
For some reason this trick just doesn't get around. What you need to do is to create a custom manifest for your jar file that defines the Main-class: property, and include in the jar file all the class files you're using. Then all you need is to run
$ java -jar myapp.jar
On real operating systems, you can even just run the jar file, as they will use the magic number to start it. But if you must, a one-liner batch or shell-script containing that line is all that's needed.
This is described in one of the Java Tutorials.
Shell scripts and batch files are the standard way of accomplishing what you want. (Look at any major Java product.)
This is, of course, absolutely pathetic and ridiculous. Go Java.
Your alternative is to write a little C program that does what you need (creates the java process) and compile that for each of your supported platforms. (C is the truly platform independent language).
Either way, you will need to take platform-dependent steps to make your app feel truly at home in the OS. On OS X, you will need to make a .app bundle, on Windows you need to package atleast the icon and version info into an EXE. For Linux, well, shell scripts suffice. :-)
If you use java -jar to launch your application, you can add additional jars to the classpath by adding a Class-Path entry to the main jar's manifest. See the jar file spec for more information.
Another solution is found in the Jakarta Commons, as always : commons-launcher.
A batch file would do the job. Put the exact command you want executed into a file myProg.bat and run it.
It's pretty easy:
Write this in TurboBlenderApp.cmd:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar %1 %2
// Bear in mind saua answer about -cp and -jar
Then from the command line you just write:
TurboBlenderApp arg1 arg2
Just the way you wanted.
If in Unix/Linux replace th4e %1 %2 with $1 $2 and make sure your app has execute rights ( If you don't know how to do this, I guess you don't need it either )