I need to include .jar file when running the java class. When I use cmd on my windows, I can successfully run using the following command
java -cp .;MyLib.jar; MyClass
However when I tried to use bash on the same PC, I got error - can't find or load main class. I searched online and some say I needed to use ./* instead of just . for the current directory. So far I tried these commands:
java -cp .:MyLib.jar: MyClass
java -cp ./*:MyLib.jar: MyClass
java -cp ".:MyLib.jar:" MyClass
java -cp "./*:MyLib.jar:" MyClass
only getting "ClassNotFoundException" every time.I know the class exists because if I switch to cmd when it runs fine. I was also able to run the class on bash shell on a mac using the following command
java -cp .:MyLib.jar: MyClass
Then I built a simple class and ran in bash without classpath
java MyTest
So using -classpath caused the issue. How do I include the current directory in bash? Why did it work on bash on a mac? Is it because I was running bash on windows? I am so puzzled by this problem.
And if it helps, I can compile in bash just not able to run the class. javac -cp MyLib.jar MyClass.java compiled the file in bash. I am working under the same directory where MyClass.class is.
Edit: the solution is java -cp ".;MyLib.jar;" MyClass. I thought I posted here for anyone who ran into the same issue as I did. Please see comments for explanation. Thank you torek!
When you write a simple Java Application in Eclipse in automatically compiles those files and stores them in the bin/ folder of the root folder of the project.
Now if I navigate to the /bin folder and to the folder that contains the .class file I want to run via the java command below I am getting the following error - :
java A
Error: Could not find or load main class A
Class A:
package assurance;
public class A {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
While the class A has a main method and runs fine when I right click on the file and do a Run As Java Application. But it does not run from the command like java command.
Why is this happening ?
Update:
Tried with the following commands-:
java -cp "A.class" assurance.A
java -cp "A" assurance.A
java -cp "*" assurance.A
It works in Eclipse, because Eclipse just runs it with correct -cp and correct command :)
Run your code with the following command:
java -cp "./" assurance.A ("" for some odd cmd interpreters like Windows XP)
it is important that the command is run from the "default package" directory (top-level package directory).
Java interprets package name (assurance) as directory path to the class file. Imagine if it replaces . with / and adds .class extension
(assurance.A => ./assurance/A.class)
More details here: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
I have source code for QR code generation using Java.
I don't know how to run Java in command prompt.
two jar files
E:\QR Code\lib\core-2.2.jar
E:\QR Code\lib\javase-2.2.jar
Java code path
E:\QR Code\src\com\javapapers\java\QRCode.java
Source code
http://javapapers.com/core-java/java-qr-code
Explain me how to run the java in command prompt
Regards
Karthikeyan K
To make sure that Windows can find the Java compiler and interpreter:
Select Start -> Computer -> System Properties -> Advanced system settings Environment Variables -> System variables -> PATH. ...
Prepend C:\Program Files\Java\jdk1.6.0_27\bin; to the beginning of the PATH variable.
Click OK
Then open command prompt and check if Java path setup done by below command
java -version
If everything ok it will show you which Java version you have installed .
then you can run java program Like below a example
C:\test> javac HelloWorld.java
I have test folder and it have HelloWorld.java file inside it after this
C:\test> java HelloWorld
It will give you output in my file i have added below content
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
I am very new to linux environment.
I am trying to run an simple hello world java class in linux environment.
Hello .java
package com.util;
public class Hello {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("hi");
}
}
I have compiled java class in windows environment and uploaded the .class file to linux system into /home/scripts path.
my command is as follows,
java -cp /home/scripts com.util.Hello
when i am executing this command from this same /home/scripts where Hello.class is there i am getting,
Error: Could not find or load main class com.util.Hello and not able to proceed further.
can some one help me in this issue?
navigate to /home/scripts using terminal
javac com/util/Hello.java
then
cd /home/scripts
java -cp . com.util.Hello
Or,
java -cp "/home/scripts" com.util.Hello
At first you must generate your .class file :
javac ./hello.java
This command has generated hello.class file
And after you can run your class file ! :)
java hello
We first know javac command work well.
I also met this error,and i have resolved this.Let me share this.
First we need to find the parent path of your package in your java codes.
Then cd to that path using java package + fileName should work well at that moment.
I had the exact same issue on windows, and I solved it by adding path "." to both CLASSPATH and PATH, maybe you can try this on Linux as well.
Your .class file should not reside in /home/scripts/, but in /home/scripts/com/util/. Take a look at this document that explains the relation between classpath, packages and directories.
Before Specifying the path,ensure you follow these three things meticulously,
1. Close the command prompt window, before specifying the path.
2. When adding path, add bin and semi- colon at the end and
3. If JAVAC command has worked properly, try java -cp class name.
if you want to run program in current working directory where your class reside.
java gives three options.
first option
java -cp Tester
Second option for current working directory
java -cp . Tester
Third option export CLASSPATH variable
export CLASSPATH=$CLASSPATH:. (this is the best one if your directory changes) or
export CLASSPATH=$PWD
or
export CLASSPATH=
after that you must sorce the bashrc or bashprofile.
I got a strange problem in my windows 7 system running jdk1.6.0_33
When I try to run a simple java program from command prompt, it opens a new window (something like java frame) and suddenly disappears. There is no result shown on command prompt also I am unable to terminate the process (using Ctrl+C) or close command prompt after this. A java process is created each time I do this. I tried to kill process using Task Manager, but that too didn't work.
I am able to run the same program using eclipse.
Here is my program
class HelloWorld{
public static void main(String [] args)
{
System.out.println("Hello World");
}
}
Environment variables are set as follows:
Path=C:\Program Files\Java\jdk1.6.0_33\bin
classpath=.
Commands I used are,
javac HelloWorld.java
java HelloWorld
Why is this happening? Thanks in Advance.
I'm not sure, but I think this is what happens if you use java versus javaw from the command prompt on Windows:
References:
The java command manual page for Windows.
Difference between java/javaw/javaws
If your java class path is configured correctly.
type javac command in command prompt
You will get java compiler information.
After compile the class find your folder location if any .class file is created.