How does java locate a JAR file? - java

The JAR file locates at C:\Workbench\jars\antlr-4.4-complete.jar
The environment variables are:
CLASSPATH=.;C:\Workbench\jars\*
PATH=C:\Workbench\jars;...
I am trying with the following commands:
java -jar "C:\Workbench\jars\antlr-4.4-complete.jar" <-- OK
java -jar antlr-4.4-complete.jar <-- FAIL!
java org.antlr.v4.Tool <-- OK
I am totally confused about the failed one. I am expecting the PATH variable will be looped through to locate the jar file. But it seems not. Why?
My guess is, the implementation of java -jar command line doesn't use the PATH variable for searching jar file. But still, why?

The PATH is for looking up the command to execute, in this case java will be looked up on the PATH.
You will need to supply either an absolute or relative path to java -jar because the terminal (bash/windows/zsh/etc...) will not expand arguments in this way. CLASSPATH is used by Java to look up further jars, but it expects a correct path to the initial jar as the first argument.

Related

Java project running on eclipse but giving error while using batch

I made a project for my uni. where I need to pass command line arguments. It is running perfectly fine on eclipse but when I run it using a batch file.
my batch file looks like
set path = "c:\Program Files\Java\jdk-14.0.2\bin";
javac FileHand.java
java FileHand DirectBuffer 1024 Sample.txt
pause
Do not set %path% at all. If you want to 'hardcode' the full path to java, then do so; write C:\program files\....\javac, or SET JAVA_LOC=... and then %JAVALOC%\javac. But, this is clearly is not needed; you messed up your SET PATH statement and yet javac is being invoked, so, you should probably just remove the entire 'set path' line.
The problem is classpath. There is a file named DirectBuffer.class. It is somewhere - you said that 'it works in eclipse', which means eclipse can find this file, because you told it where it is. You need to tell javac where it is. You do this as follows:
javac -cp LOC1;LOC2;LOC3 FileHandjava
java -cp .;LOC1;LOC2;LOC3 FileHand DirectBuffer 1024 Sample.txt
where LOC1 is a path. It can be a directory, or a jar file. Your question does not make this clear, but let's say DirectBuffer' is in the com.foo.pkgpackage (so, you haveimport com.foo.pkg.DirectBuffer;` in your source file), then:
either:
cd (whatever you put for LOC1)
cd com\foo\pkg
dir
should print, amongst other things, 'DirectBuffer.class', or, if LOC1 is a jar file:
jar tvf (the jar file listed in LOC1)
should print com/foo/pkg/DirectBuffer.class, amongst other things. You've already told eclipse this, so now find those places where you did that and tell javac about it.

How to use a .jar from Command Line without typing full path

I downloaded example.jar and I can type java -jar example.jar from within the directory where it is located and it works.
The problem is that I need to be able to call it from elsewhere without typing the full path. Is it possible?
I tried adding it to $CLASSPATH like this:
export CLASSPATH=$CLASSPATH:/Path/to/Directory:/Path/to/Directory/example.jar with no success.
Yes. Option 1. Using the CLASSPATH you have set, however you would have to specify the fully qualified main-class from the jar
java com.mypackage.MyMain
As long as com.mypackage.MyMain is on the CLASSPATH and contains a valid main method, that will run it.
Option 2. Create a bash shell script to run it (note that this is really providing the full path to the java command)
#!/usr/bin/env bash
export JARFILE="/Path/to/Directory/example.jar"
java -jar $JARFILE

How to test that a Jar is in a classpath in windows?

I have the classic :
java.lang.NoClassDefFoundError: org/python/util/jython
Even if the jar jython.jar exist in the environment variable PATH for my user and system wide path.
How can I quickly test that I am not crazy and it is indeed in the path?
Java doesn't use the PATH variable.
To specify the classpath when running a java application use the -cp parameter for the java command.
Inside your batch file (the .cmd file) find the java command and add the needed jar file:
java -cp somefile.jar;\path\to\jython.jar someclass.MainMethod
Please don't use the deprecated CLASSPATH any more.
For more details, please see the Java documentation:
http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html#tooloption
use the following command
set classpath="path to your jar/jython.jar";

Run java program sing Cmd

I'm trying to run Java program using cmd.
When I compile that file using
javac helloworld.java
It works perfectly but when I try to run that file using
java helloworld
I get an error:
couldn't find or load main class.
even though my I put my javac path in class path in system variables and javac working correctly.
After searching on how I can fix it I found that I can use
java -cp . helloworld
because it let you to find .class file.
and it works but I know that I can run java program without this -cp so what this for and how I can run my program without it?
-cp specifies the Java classpath for the JRE you are attempting to start. Look for an environment variable CLASSPATH and add '.'.
-cp is used to set the classpath for the jar file, this flag is same as importing a jar file to eclipse and then use it.
If you want to run without this flag, use should set the classpath first beforing running.
export CLASSPATH=path/to/your/jarfile.jar
If you already have some classpath set
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar
If you want to include current directory
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar:.
As other have mentioned, you can set the CLASSPATH. However, a much better approach is to bundle the .class files in a JAR ans user java -jar nameofthejar.jar.

How to execute an external program

I'm trying to execute a java program from a python program :
subprocess.Popen(["java -mx256m -jar /sphinx4-1.0beta5/bin/HelloWorld.jar"], shell=True)
but it fails with this error:
Error: Unable to access jarfile /sphinx4-1.0beta5/bin/HelloWorld.jar
i need to be in an specific directory : /home/karen/sphinx4-1.0beta-src, to execute the command:"java -mx256m -jar /sphinx4-1.0beta5/bin/HelloWorld.jar"
But i don't know how to do this. I need that my python program execute it !
use cwd parameter
subprocess.Popen(["java -mx256m -jar ../sphinx4-1.0beta5/bin/HelloWorld.jar"], cwd=r'path', shell=True)
http://docs.python.org/2/library/subprocess.html
"If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd."
Your problem is probably related to your path to the jar file. Your code should most likely call /home/Karen/sphynx4-1beta-src in your popen call. This isn't a solution that will work on a different system, unless the file is in the same absolute path though.

Categories

Resources