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)
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 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"
I am trying to extract a jar file from Matlab code using javac from Library Compiler (java package).
I have set JAVA_HOME to:
C:\Program Files\Java\jdk1.7.0_71
and added to PATH:
C:\Program Files\Java\jdk1.7.0_71\bin.
When I enter java -version in my console, I get java version jdk1.7.0_71 and running javac -version shows jdk1.7.0_71.
However, it seems that matlab could not find javac, thus, I am not able to compile my .m code into a .jar file. When I tried to compile .m code I got the following:
Error: An error occurred while shelling out to javac (error code = -1).
Unable to build executable.
Executing command: ""C:\Program Files\Java\Java\jdk1.7.0_11\bin\javac" -verbose - classpath "...\MATLAB\R2014a\toolbox\javabuilder\jar\javabuilder.jar" -d "...\features_extraction\for_testing\classes" "...\features_extraction\for_testing\features_extraction\Class1.java" "...\features_extraction\for_testing\features_extraction\Features_extractionMCRFactory.java" "...\for_testing\features_extraction\Class1Remote.java" "...\for_testing\features_extraction\package-info.java""
This is the directory of my java bin folder and javac.exe exists : ...\Java\jdk1.7.0_71\bin\bin\javac? I found this link for compatibility between Java and Matlab2014a(which is my version) here. What should I check in order to correctly link Matlab to Java? A few days ago Matlab was working correctly with Java.
In my PATH I noticed the existence of the following path: C:\ProgramData\Oracle\Java\javapath in which there are three wrong shortcuts to java.exe, javac.exe, and javax.exe. When I type in matlab console getenv JAVA_HOME I am getting C:\Program Files\Java\jdk1.7.0_11 while when I wrote version -java I got: Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode. When I write javac in matlab console I am getting: Undefined function or variable 'javac'.
When I try to built C++ library I got a similar issue: Error: An error occurred while shelling out to mbuild (error code = -1).
Unable to build executable.
EDIT: Does the absence of the proper MCR counts in my case?
You said
getenv JAVA_HOME I am getting C:\Program Files\Java\jdk1.7.0_11 while when I wrote version -java I got: Java 1.7.0_11-b21
try to use setenv in MATLAB, doc here http://www.mathworks.com/help/matlab/ref/setenv.html
setenv('JAVA_HOME','C:\Program Files\Java\jdk1.7.0_71');
setenv('PATH','C:\Program Files\Java\jdk1.7.0_71\bin');
Following from the answers in this link
http://www.mathworks.com/matlabcentral/answers/131639-matlab-deploytool-not-working-fails-while-building
seems like you have to install JDK 1.7.0_11-b21, which is the one your MATLAB version is compatible with for compiling into jar. Also, after installing that specific version, you should modify the JAVA_HOME and PATH environment variables. Try that to see if it helps.
I was having a very similar problem with my installation.
I set the environment variable (press > type env > Enter) JAVA_HOME (under "User variables") to my JDK home directory (not the bin folder) without any trailing \ or ;.
The problem occurs because MATLAB cannot execute javac properly, however, in order to work out exactly what MATLAB is trying to execute (which is not working) we need to use the command window and not the Library Compiler.
It appears you have already done this but for completeness I will add the process here for others.
To find what MATLAB executes, click "Open log file" and copy the entire first line.
Paste the first line in the MATLAB command window and add -v to the end.
Press enter to run the command and note the "Executing command:" that is printed to the screen.
Once you have the "executing command" that MATLAB returns for you, check the first path in that command and make sure it matches your javac file.
In your original post you have done this and posted
C:\Program Files\Java\Java\jdk1.7.0_11\bin\javac
a path which contains \Java\Java, if this isn't a typo then it may be a problem as it is not the default Java installation path. You also say after that your installation is at
...\Java\jdk1.7.0_71\bin\bin\javac
which contains \bin\bin and is also not the default java bin install directory, perhaps another typo?
If the path to javac matches exactly you could try copying and pasting the "executing command" into your OS's command window (+R > cmd > Enter) and looking for errors. By doing this we can remove ourselves from the MATLAB installation and if errors still exist it likely has nothing to do with MATLAB.
To see if the same problem could be caused by other environment variables I removed every other path to a java installation (keeping JAVA_HOME) in my environment variables. MATLAB uses the absolute path to the java installation and so it was still able to compile with javac using only the JAVA_HOME environment variable.
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