Running Java from another folder - java

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

Related

Can't specify class path in 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.

"java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver" error when running from terminal

I have a program that I run from Eclipse successfully.
However, when I want to run it from terminal, I encounter the famous error:
"java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver"
on this line:
Class drvClass = Class.forName("oracle.jdbc.driver.OracleDriver");
PS:
I have the following in CLASSPATH:
/oracle/jdbc/lib/ojdbc6.jar
Also note that I compile it successfully (javac Test2.java). Then when I run it (java Test2), I get the following error:
Error: Could not find or load main class Test2
So I run:
java -classpath ~/Desktop/JDBC2/src Test2
It runs, but I get the above "ClassNotFoundException" though.
I found this question tricky: the reason is related to semicolon after jar file address.
At first I changed the directory of MySample.java to another directory (you can don't do that) like C:\
then I removed package address from the source code, at the end I run this command in cmd
java -cp path_to_oracle_driver.jar; MySample
P.S. If you want run it from terminal you have to remove package PackageAddress from the source code and compile it again.
As #yngwietiger mentioned above in the comments, using -classpath parameter when running the .class file, overrides the original CLASSPATH and the predefined ojdbc6.jar file. So we need to mention both when running:
java -classpath ~/Desktop/JDBC2/src:/oracle/jdbc/lib/ojdbc6.jar Test2
Or, as a better solution, we can add the current path to CLASSPATH (note the colon and dot at the end):
export CLASSPATH=$CLASSPATH:.
And, in order to run, we just need to type:
Java Test2

"Could not find or load main class" Error while running java program using cmd prompt [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 2 years ago.
I am running a simple "HelloWorld" Program. I get this error in the command prompt:
Could not find or load main class HelloWorld.
I have set the CLASSPATH and PATH variable in the system. In the cmd prompt, I am running from the directory where I have saved HelloWorld program. I can see the class name and the file name are same and also .class file created in the same directory. What else could be the problem?
My sample program looks like this:
package org.tij.exercises;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World!!");
}
}
When the Main class is inside a package then you need to run it as follows :
java <packageName>.<MainClassName>
In your case you should run the program as follows :
java org.tij.exercises.HelloWorld
What's your CLASSPATH value?
It may look like this:
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
I guess your value does not contain this .;.
So, ADD IT .
When you done , restart CMD
That may works.
For example the file HelloWorld.java is in path: D:\myjavatest\org\yz\test and its package is: org.yz.test.
Now, you're in path D:\myjavatest\ on the CMD line.
Type this to compile it:
javac org/yz/test/HelloWorld.java
Then, type this to run it:
java org.yz.test.HelloWorld
You may get what you want.
I removed bin from the CLASSPATH. I found out that I was executing the java command from the directory where the HelloWorld.java is located, i.e.:
C:\Users\xyz\Documents\Java\javastudy\src\org\tij\exercises>java HelloWorld
So I moved back to the main directory and executed:
java org.tij.exercises.HelloWorld
and it worked, i.e.:
C:\Users\xyz\Documents\Java\javastudy\src>java org.tij.exercises.HelloWorld
Hello World!!
Since you're running it from command prompt, you need to make sure your classpath is correct. If you set it already, you need to restart your terminal to re-load your system variables.
If -classpath and -cp are not used and CLASSPATH is not set, the current directory is used (.), however when running .class files, you need to be in the folder which consist Java package name folders.
So having the .class file in ./target/classes/com/foo/app/App.class, you've the following possibilities:
java -cp target/classes com.foo.app.App
CLASSPATH=target/classes java com.foo.app.App
cd target/classes && java com.foo.app.App
You can check your classpath, by printing CLASSPATH variable:
Linux: echo $CLASSPATH
Windows: echo %CLASSPATH%
which has entries separated by :.
See also: How do I run Java .class files?
I had the same problem, mine was a little different though I did not have a package name. My problem was the Class Path for example:
C:\Java Example>java -cp . HelloWorld
The -cp option for Java and from what I can tell from my experience (not much) but I encountered the error about 20 times trying different methods and until I declared the class Path I was receiving the same error. Vishrant was correct in stating that . represents current directory.
If you need more information about the java options enter java -? or java -help I think the options are not optional.
I just did some more research I found a website that goes into detail about CLASSPATH. The CLASSPATH must be set as an environment variable; to the current directory <.>. You can set it from the command line in windows:
// Set CLASSPATH to the current directory '.'
prompt> set CLASSPATH=.
When you add a new environment setting you need to reboot before enabling the variable. But from the command prompt you can set it. It also can be set like I mentioned at the beginning. For more info, and if your using a different OS, check: Environment Variables.
One reason for this error might be
Could not find or load main class <class name>
Maybe you use your class name as different name and save the class name with another name you can save a java source file name by another name than class name. For example:
class A{
public static void main(String args[]) {
System.out.println("Hello world");
}
}
you can save as Hello.java but,
To Compile : javac Hello.java
This will auto generate A.class file at same location.
Now To Run : java A
Execute your Java program using java -d . HelloWorld command.
This command works when you have declared package.
. represent current directory/.
I had a similar problem when running java on win10
instead of
$ java ./hello
Error: Could not find or load main class ..hello
Run
$ java hello
Hello, World
I was getting the exact same error for forgetting to remove the .class extension when running the JAVA class. So instead of this:
java myClass.class
One should do this:
java myClass
I used IntelliJ to create my .jar, which included some unpacked jars from my libraries. One of these other jars had some signed stuff in the MANIFEST which prevented the .jar from being loaded. No warnings, or anything, just didn't work. Could not find or load main class
Removing the unpacked jar which contained the manifest fixed it.
I faced the same problem and tried everything mentioned here.
The thing was I didn't refresh my project in eclipse after class creation .
And once I refreshed it things worked as expected.
faced the same problem. solved by following these steps
go to directory containing the package 'org.tij.exercises' (e.g: in eclipse it may be your src folder)
use java org.tij.exercises.HelloWorld
For a lot of us, at least for me, I think the class path hierarchy is not intuitive since I'm working inside a directory structure and it feels like that ought to be it.
Java is looking at the name of the class based on it's package path, not just the file path.
It doesn't matter if:
i'm in the local directory ./packagefoo/MainClass, or
a directory up ./packagefoo/, or
one down ./packagefoo/MainClass/foo.
The command "java packagefoo.MainClass" is running off the root %CLASSPATH% which means something significant to Java. Then from there it traverses package names, not path names like us lay coders would expect.
So if my CLASSPATH is set to %CWD%/, then "java packagefoo.MainClass" will work.
If I set the CLASSPATH to %CWD%/packagefoo/ then packagefoo.MainClass can't be found.
Always "java MainClass" means nothing, if it is a member of "package", until I rip out the java code "package packagefoo;" and move the Class File up a directory.
In fact if I change "package packagefoo;" to "package foopackage;" I have to create a subfolder under CLASSPATH/foopackage or foopackage.MainClass stops working again.
To make matters worse, Between PATH, CLASSPATH, JAVAHOME, for Windows, JDeveloper, Oracle Database, and every user name it was installed under, I think a coder trying to just get something up fast ends up brute forcing path variables and structure until something works without understanding what it means.
at least i did.
Create a folder org/tij/exercises and then move HelloWorld.java file. Then run below command
javac -cp . org/tij/exercises/HelloWorld.java
AND
java -cp . org/tij/exercises/HelloWorld
I was facing similar issue but it was due to space character in my file directory where I kept my java class.
Scenario given below along with solution:
public class Sample{
public static void main(String[] args) {
System.out.println("Hello world, Java");
}
}
My Sample.java class was kept at Dir "D:\Java Programs\Sample.java"[NOTE: Package statement not present in java class].
In command prompt, changed directory to "D:\Java Programs\", my programmed compiled but failed to run with error "Could not find or load main class"
After all the possible solutions over SOF(nothing worked), I realized may b space causing me this issue.
Surprisingly removal of folder name space char['Java Programs' -> 'JavaPrograms'], my program executed successfully. Hope it helps

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.

Cannot run simple compiled java program?

I am on Arch Linux, I just installed JRE and JDK and all the proper bin files (javac and java) are in /opt/java/bin/
I simply compiled a standard hello world, and compiled it with javac running javac ./hello.java and that made a class.
Now my problem is running it. I run java ./helloworld.class and it gives me an error, even if the file I point java to is non-existant:
Exception in thread "main" java.lang.NoClassDefFoundError: //helloworld/class
Caused by: java.lang.ClassNotFoundException: ..helloworld.class
(..omitted for clarity..)
Could not find the main class: ./helloworld.class. Program will exit.
You will notice the first line of the error, it munges the path //helloworld/class
When I feed java an absolute path, i.e java /home/foo/helloworld.class it gives the same error, but replaces the path's / with . in the first line, again munged.
What do you think is wrong? I really don't know why it is doing this..
When you run java, you just pass it the fully qualified class name (including package), not the file name.
java helloworld will look for helloworld.class.
java helloworld.class will look for helloworld/class.class
You do not run a file as
# java file.class
you run it as
# javac PATH/file.java
# java PATH/file
Do not add .class while using JAVA command.
Actually you should compile it like this
javac helloword.java
run the program
java helloword
And yet another thing: add command line option "-classpath ." or it short version "-cp .", i.e. your command line should look like:
java -cp . helloworld
this is if your class is in your current directory. Otherwise "." should be replaced by path where the class(es) may be found.

Categories

Resources