Can't specify class path in Java - java

I have a Test.jar file with ru.package.Tester class in it. When I run the following command in the directory with my jar:
java -classpath /path/to/current/directory ru.package.Tester
I get the following error message:
Could not find or load main class
I'm running on OS X
upd: When I put my jar file into /Library/Java/Extensions, it all works without specifying -classpath.

You need to give path to jar
java -classpath /path/to/current/directory/your.jar ru.package.Tester

could you please check your ~/.bash_profile - what does it contain . I too faced the similar issue on my mac yesterday and got it resolved.

Forget about the classpath. By default, Java looks for the classes directly in the directory. If you want to run a jar, you must tell Java to do so:
java -jar your.jar

Please check manifest file. it must contain main class name in manifest.

Related

how to run a java class in another folder with jar file

i'd like to run a java class on other folder, i have a mysqlcon.jar on the current path and a PetsGUI.class on ./classes/
when i try to run it by doing
java -cp .:mysqlcon.jar -d classes/PetsGUI
i receive
classes/PetsGUI not found
if i move mysqlcon.jar on classes and type
java -cp .:mysqlcon.jar PetsGUI
on classes/ it runs, so the code is correct. what's the correct command to run it?
So you need the jar file, and the classes directory in the classpath:
java -cp ./classes:mysqlcon.jar PetsGUI
java doesn't expect a file path as argument. It expects the fully quelified name of a class. And that class is then searched on the classpath.

Running Java from another folder

I wrote some Java code. I can run it from the target folder as such:
java -classpath dependency/j1.jar:dependency/j2.jar package.name.here.className
and this is in folder /usr/local/proj/api/target/.
Now, I am trying to call the same java program from ~/usr/local/proj/. So I figure appending the path to the dependency and the package would work:
java -classpath /usr/local/proj/api/target/dependency/j1.jar:/usr/local/proj/api/target/dependency/j2.jar /usr/local/proj/api/target/package.name.here.className
But instead I am getting an error:
Error: Could not find or load main class .usr.local.proj.api.target.package.name.here.className
I have tried to remove the path on the classname but the same error still exists. Please assist!
EDIT: Few questions: What is the appropriate syntax before the classname? periods or slashes?
try to use this syntax
java -classpath /usr/local/proj/api/target/dependency/j1.jar:/usr/local/proj/api/target/dependency/j2.jar package.name.here.className
Put the target path in the class path.
java -classpath /usr/local/proj/api/target:/usr/local/proj/api/target/dependency/j1.jar:/usr/local/proj/api/target/dependency/j2.jar package.name.here.className

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.

Running Java Program From Command Line

so I am having a noob moment here, I haven't ever used the command line to run a java program before but I need to right now. The problem I am having is that when I try to run the program I get a ClassNotFoundException. My class is called OmadUpdate. I already have compiled the OmadUpdate.java file into OmadUpdate.class using the javac command. I have checked the directory and they are both definitely there, however when I run the java OmadUpdate command, it gives me an error message saying
Exception in thread "main" java.lang.NoClassDefFoundError: OmadUpdate (wrong name: org/openmetadata/main/OmadUpdate)
......
......
Could not find the main class: OmadUpdate. Program will exit
But its right there in the directory. When I type dir I have both OmadUpdate.class and OmadUpdate.java. I have even tried using "java org.openmetadata.main.OmadUpdate" because that is the package name that it is under. I am stumped. Thanks for the assistance.
Your class appears to have been declared in the org.openmetadata.main package.
To get java to load the class correctly, it needs to be in the correct directory structure that matches the package structure.
So the classfiles for org.openmetadata.main.OmadUpdate should be in the directory org\openmetadata\main.
Then, when you run the java command, the root of this directory structure should be on the classpath - for a simple example this just means that your current directory should be the parent directory of org\openmetadata\main.
When running java you need to specify the full classname using periods not slashes, i.e.
java org.openmetadata.main.OmadUpdate
After you compile the class with javac, you'll have the following directory structure:
org/
openmetadata/
main/
OmadUpdate.class
OmadUpdate.java
Make sure you're in the parent directory of org, then run
java -cp . org.openmetadata.main.OmadUpdate
Class names have their nested package names separated by periods, while the directories use slashes. Odds are good you tried java -cp . org/openmetadata/main/OmadUpdate when you should have (since you are specifying a class name) tried java -cp . org.openmetadata.main.OmadUpdate
Note that for this to work, you must run it in the directory just above the org subdirectory. Otherwise that classpath directive cp . will start the search in the wrong directory.
launch your java app with the classpath set:
java -cp . org.openmetadata.main.OmadUpdate
-cp . won't do anything I don't think. You have to make sure you are invoking java in the right directory, which is the part to the first package name / folder (in your case org)
You need to use the full package and class name to run it.

Categories

Resources