I am trying to run my simple java program via command prompt. While compiling the code there is no error. But every time I execute the program, I get the same error,
C:\Users\anapi>javac simple.java
C:\Users\anapi>java Simple.class
Error: Could not find or load main class Simple.class
C:\Users\anapi>java -cp . Simple.class
Error: Could not find or load main class Simple.class
I searched in net for the solution, and there are many solutions provided. But none of them worked for me. So I've posted here for the help. I know the problem is due to class path and I tried the possible solutions too but none of them worked. Please check my JAVA configurations details.
C:\Users\anapi>java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) Client VM (build 25.101-b13, mixed mode)
C:\Users\anapi>javac -version
javac 1.8.0_101
C:\Users\anapi>echo %JAVA_HOME%
C:\Program Files\Java\jdk1.8.0_101
C:\Users\anapi>echo %PATH%
C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.8.0_101\bin;C:\Program Files\Java\jre1.8.0_101\bin
C:\Users\anapi>echo %CLASSPATH%
.C:\Program Files\Java\jdk1.8.0_101\bin;
My code:
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
Please help me I am badly stuck in this problem.
Use the command as ::
C:\Users\anapi>java Simple
Also note that if you are using Eclipse and want to compile and execute your code from command prompt then follow the following steps::
Suppose your java file is simple.java inside [C:\Users\anapi\workspace\test],
for compiling the code ::
C:\Users\anapi\workspace\test>javac C:\Users\anapi\workspace\test\src\test\simple.java
and for executing the code ::
C:\Users\anapi\workspace\test>java -cp bin test.Simple
You can get the class path of the source file by running the program in debug mode. After that, in the upper left tab [Debug], right click on your terminated program and go to Properties. There in the Command Line box you can see the classpath. Paste the classpath with java command in command prompt.
C:\Users\anapi>java -classpath C:\Users\anapi\workspace\FirstProject\bin test.HelloWorld
Write this instead:
C:\Users\anapi>java Simple
without the ".class"
Related
I know this question has been answered before but as a total newbie trying to start learning Java i'd be grateful please for a specific answer?
In Eclipse when i press run with the following code it works fine
public class MySweetProgram {
public static void main(String[] args) {
System.out.println("Hello there!");
}
}
However when i go to C:\Program Files (x86)\Java\jre1.8.0_241\bin in a command prompt and type java MySweetProgram i get an error saying cannot load or find main class MySweetProgram
I've searched through other threads that advise to set the classpath. Mine is set to C:\Program Files (x86)\Java\jre1.8.0_241\lib.
I changed the folder in Classpath from lib to bin but got the same error, so i changed it back again
I do a java -version and get the following :
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) Client VM (build 25.241-b07, mixed mode)
Would someone be able to advise me please on what they think may be the issue?
I apologise if this is an ignorant question
Thanks
Jimmy
welcome! Eclipse will be compiling the code for you and then running it. It does this all for you when you click run, to make your life as a developer slightly easier. If you would like to compile and run the command from the command line, you will need to ensure you have access to the jdk (which must be installed somewhere by Eclipse, but may not be immediately obvious where... a folder called /something/jdk/, presumably). With that on the path (in the %PATH% variable), the following should work for you:
cd /to/where/your/code/is
javac MySweetProgram.java
java -cp . MySweetProgram
Have a look into compilation and what it means, so you gain a better understanding of what Eclipse is doing. And good luck to you!
When you compile a .java file, in your case the MySweetProgram.java, you can decide where you want the executable file to go. That file will be called MySweetProgram.class. Eclipse may keep those two files in separate directories.
Normally (before Java11) you could only run a .class file. So you'd have to add classpath (-cp) argument for java to find your file:
java -cp path MySweetProgram (don't put the .class)
Now (after Java11) you can run the java file directly because when you give it a .java file, it will compile it for you just before executing it:
java MySweetProgram.java (if you're in the directory)
java path\MySweetProgram.java
If you have a MySweetProgram.class in the directory, this will give an error.
It gets a little more complicated when you use packages, but Oracle has some very nice Offical Java Tutorials
I am new to Java and I want to run a simple test-program on an Ubuntu server via the command line. "Hello World" worked well.
But now I want to add jar-files to my test program. So I followed this simple tutorial.
I created a file "MyTest.java" like this:
import org.apache.commons.lang3.*;
public class MyTest
{
public static void main (String[] args)
{
// Print Hello World!
String x = "Hello World!";
System.out.println(StringUtils.capitalize(x));
}
}
Compiling the java-file with
javac -cp jars/commons-lang3-3.7.jar MyTest.java
worked well without errors and a MyTest.class() file is generated. But when I want to run the test-program with
java -cp jars/commons-lang3-3.7.jar MyTest
I end up with the following Error:
Error: Main class MyTest could not be found or loaded
What am I missing? Why is the compiler successful but yet the program unable to be executed?
EDIT:
To make it clear: I do NOT want to build a jar file (at least not yet). I just want to build and run a simple java program referencing a jar-file i downloaded from the internet. Like described here: https://www.programcreek.com/2014/01/compile-and-run-java-in-command-line-with-external-jars/
Java Version:
> java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
JAVAC Version:
> javac -version
javac 1.8.0_151
You forgot to specify "MainClass" to the Manifest.mf
That should be in the last step of Eclipse Jar Export Wizard.
Tell me if you're on another IDE or using Maven, that changes the procedure.
Your current command should run successfully.
The solution was to include the current directory.
java -cp jars/commons-lang3-3.7.jar:. MyTest
Thought that would happen automatically. But the dot at the end of the -cp argument was necessary.
I just created a .java file.
(my IDE is NetBeans, java version java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode))
I want to compile it and then execute it from windows CMD because of the command line arguments.
So i write on CMD: javac className.java and everything works as expected.
After that i write the command: java className and this error pops out on CMD:
Error: Could not find or load main class AlgorithmsProject
Now on docs.oracle.com it says clearly on Common Problems:
"A common mistake made by beginner programmers is to try and run the java launcher on the .class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorldApp.class instead of java HelloWorldApp. Remember, the argument is the name of the class that you want to use, not the filename"
Obviously i write and the className only and it gives me the same ERROR.
On youTube some videos show that you have to write the following commands on CMD:
set path="C:\Program Files\Java\jdk1.8.0_101\bin";
set classpath="C:\Program Files\Java\jre1.8.0_101\lib\rt.jar";
and the same error still pops out.
Any suggestions?
I'm Java newbie, but I had the same problem. I don't know if it helps you but you can just try it because it helped me:
javac className.java (just like you did earlier)
go one directory up (back) and copy its path to clipboard (for example C:\Users\XYZ\Documents\Exercise1\src)
in cmd write cd and paste the copied path with right mouse button and then click "Paste" so in cmd you are now moved one directory up
(it looked this way im my example:
cd C:\Users\XYZ\Documents\Exercise1\src)
java packageName.className (where packageName is the name of the folder containing your java file and the name you gave to the package when creating new project). In my example java file was under the following path:
C:\Users\XYZ\Documents\Exercise1\src\Exercise1\className.java)
I have a simple, single file java program that relies on a single static jar. The java code and the jar reside in the same directory. For this one-off solution I don't want to bring in the weight of ant or maven, and just want to compile it directly.
On my dev box, the following compiles and runs my code fine:
javac -cp ".;dependency.jar" File.java
java -cp ".;dependency.jar" File
However, on my test box, the java command fails, and I get the following output:
Error: Could not find or load main class File
If I change my classpath arg to -cp "." I get the following output:
Exception in thread "main" java.lang.ClassNotFoundException: dependency
My dev box is 64-bit Windows/Cygwin and java version 1.7.0_55. My test box is 64-bit Linux and java version 1.7.0_45.
What is going wrong on my test box?
The classpath separator character is different on Linux (and on Unix) than it is on Windows. It's ; on Windows, but it's : on Linux (and Unix).
Try this on Linux:
javac -cp ".:dependency.jar" File.java
java -cp ".:dependency.jar" File
I'm trying to wrap a program of mine to work with java.
I tried a simple "hello world" first,
-hello world.m-
disp('hello world');
I used deploytool and selected java package.
when it reached this line:
Executing command: "javac -verbose -classpath "C:\Program Files\MATLAB\R2009b\toolbox\javabuilder\jar\javabuilder.jar" -d "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\classes" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\helloworld.java" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\DeployTutorial2MCRFactory.java" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\helloworldRemote.java" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\package-info.java""
I got this error:
'javac' is not recognized as an internal or external command,
operable program or batch file.
Error: An error occurred while shelling out to javac (error code = 1).
Unable to build executable.
btw: when I tried standalone application / c/c++ shared library it has been compiled successfully.
thanks in advance
Possibly the Java SDK is not installed or properly configured on your machine. Open a system terminal and execute the following two commands:
java -version
javac -version
If they both work you should proceed with the examples from the MATLAB help. If not install the Java SDK.
First you should install JAVA.
Then you must set the environment variable in "my computer"
Add a new variable named "JAVA_HOME" and set its value to your jdk path
like D:\Program\Java\jdk1.6.0_25
Then restart your matlab
and type
getenv JAVA_HOME
you should get
ans=
D:\Program\Java\jdk1.6.0_25