Java Program not running in command prompt - java

Well, I'm able to compile my program in command prompt using
javac Main.java
It is compiling without any hassles. But the problem arises when i try to run the program. Whenever i try running it in command prompt after compiling, i get an error message
Exception in thread "main" java.lang.NoClassDefFoundError: main
What does this mean?
And I have set my classpath and path variables too. As far as I know i seem to have covered the basics but i don't know what I'm missing.

could this be a uppercase lowercase typo? the filename and the classname as well the name when you run your program via java should all have the same name.
in your case, probably Main with an upper case M.

First,after you executed the javac ,you should check the existence of class file of main Java file

Related

Having trouble with java.lang.NoClassDefFoundError (wrong name:) when running from cmd

I'm new to Java, and as the title says I'm struggling to run my Test program (it's basically just a hello world program) from the cmd, I'm using the directory of myfolder\eclipse\learning\src\learning to store my project classes/.java files.
I get the error
Caused by: java.lang.NoClassDefFoundError: learning/Test (wrong name: Test)
I'm not sure why, I've done "javac Test.java", and that goes fine, but whenever I try "java Test" I get that error. I've also tried "java Test.class" but that just gave me the error
Caused by: java.lang.ClassNotFoundException: Test.class
The class also runs fine in Eclipse. I've googled this problem many times and have found no solution, but I could just somehow be using those solutions incorrectly.
Thank you for your time.
I assume that the first line of Test.java is:
package learning;
To run this class after you compile it, you need to be in the myfolder\eclipse\learning\src directory. Then run it with java learning.Test. This is because you have created the class inside a package as shown by the package declaration. Eclipse is built to understand packages and does the right thing automatically for you. That's why you don't get any errors running in Eclipse.
Compile is as follows:
myfolder\eclipse\learning\src\learning\>javac -d . Test.java
Run it as follows:
myfolder\eclipse\learning\src\learning\>java learning.Test
Notes:
-d specifies where to place generated class files.
. stands for current directory
Note that if you are using Java 11, you can run it directly as java Test.java even without compiling it. Check this to learn more about it.

How can I run multiple threads in JOMP on Windows (10) from either the command prompt or in Eclipse?

I can't get multiple threads to run using JOMP no matter what I try. I actually can't run a JOMP program from the command line no matter what I try either in fact, although ironically it will compile from there and then run in Eclipse! Even in Eclipse though I only have one thread. I've been through the notes from my university course about installation of JOMP carefully, but they have not helped. I'll be more specific though:
Items in quotes below are from those notes:
"There are a couple of websites that tell you how to make jomp run under Eclipse, see http://www.lst.inf.ethz.ch/teaching/lectures/ss10/24/ assignments/assignment_10/eclipse.txt"
This refers to a now broken link. It also seem to be the only link anyone on forums like Stackoverflow refer to when talking about this issue. Apparently it has instructions on runtime settings for Eclipse to allow multiple threads to run, but since the link is currently broken I can't access those valuable instructions.
"All that is required in order to do that is to ensure that jomp1.0b.jar is on the CLASSPATH"
I ran echo %CLASSPATH% at the command prompt to check if it was on the class path and got the following response:
C:\Program Files\Java\jre1.8.0_162\lib\jomp1.0b.jar
On my PC the jomp jar file is in that folder, so it appears I should be able to execute compiled JOMP programs from the command line, but unfortunately that is not the case. By executing one of these commands it should run:
java −Djomp.threads=2 parallel
java −Djomp.threads=2 -cp . parallel
java −Djomp.threads=2 -cp C:\Users\terry\eclipse-workspace\JOMPHello\src parallel
This is the folder the jomp, java and compiled class files are in. I also checked if "parallel" is the fully qualified class name in the way I have set it up in Eclipse, and it does appears to be. So running one of these commands should allow me to run the jomp program from the command line as near as I can tell, but they all return the following error:
Error: Could not find or load main class parallel
Caused by: java.lang.ClassNotFoundException: parallel
(To which I feel like telling Java, "You're not looking hard it enough! It is right in the folder I am running this command from!")
Clearly I am missing something. Can anyone tell me how to get JOMP programs running on the command line, or alternatively knows where there are accessible instructions for how to set up the work around runtime settings in Eclipse?
My implementation of the program seems to run with only one thread, so hopefully that means it is correct, but I can only be sure once I have run it with at least a few more threads.
Thanks,
Terry.
I figured out how to set up the runtime argument in Eclipse. You just have to add the following line into the VM Arguments box in under the Argument tab in Run Configurations for the file:
−Djomp.threads=n
(where n as before is the number of threads you want).
I'd still like to know why it's not working on the Command Line though. It makes me think my Java is set up weirdly.

Batch File calling Java Issue

I currently have an open instance of the command prompt, it reads
java -cp .;jars/GUI.jar;jars/utils.jar;...(a bunch more jars in the same folder)... Test
where Test is my java program (class files already compiled).
When I hit enter, the program runs just fine. Now, I put the same exact line into a batch file for automatic runs but it gives me an error.
Batch File:
cd
java -cp .;jars/GUI.jar;jars/utils.jar;...(a bunch more jars in the same folder)... Test
pause
Error: Exception in Thread "Main" java.lang.NoClassDefFound Test (wrong name: Test)
I've been at this for hours today so at this point, this is the last thing I need to do, my brain is fried, am I missing something simple?
Or the Bash script doesn't know where the PATH to the Java installation. I'd check the environment variables as well.
Maybe you should try providing the full path of Test class: /x/y/Test
I'm not really sure of the exact rules but i guess the computer can't know which class you want to launch if Test.class is not in the current folder (current script folder? current cd/home folder?)

Please help me get the java command to work

I'm learning how to make and run Java programs in Windows with Notepad and the command line. Right now I'm getting a well-known error when I try to run any sort of variant of java Main, Main being the starting class of the program, and I've done some research on it. The error is java.lang.NoClassDefFoundError, but any of the normal solutions for this error don't seem to be helping.
The program is in <root>\com\zork, and the classes are Main, Dungeon, and DungeonMaster, each in the package com.zork. They've all compiled just fine with the javac command, but attempting to run the program with java Main in either <root>\com\zork or just <root> keeps giving me the error above. I've tried many things with -cp, -classpath, and where I'm running java from. The current classpath is
<this directory doesn't matter>;.;"C:\Users\intprof\Desktop\ZORK in Java",
although the last entry has also been C:\Users\intprof\Desktop\ZORK in Java\com\zork, nonexistent, and a couple of other things.
What am I doing wrong? Thanks!
You need to run java com.zork.Main.

compile and execute java file batch file problem

Given this .bat file :
#ECHO OFF
echo === Compiling and executing bat file ===
md C:\my_project\dir_prog\class_files
copy ProgAudioJ.java C:\my_project\dir_prog
javac -d C:\my_project\dir_prog\class_files ProgAudioJ.java
java -classpath C:\my_project dir_prog.class_files.ProgAudioJ
I'm wondering what's wrong with it.
It just doesn't work.
I mean it places the class files in the directory called class_files (so the compiling process is ok) but the program doesn't run...
Thanks for your help MAX
Do you have defined a main class within your project and do you have defined the
public static void main(String[] args) method for this class? Remember, that method gets
called when running stuff from shell...
EDIT: you might find this comprehensive overview usefull: Running a Java Program from Command Prompt # SkylightPublishing.com
HTH
I think the first problem is that you need to specify the class_files directory on the classpath.
The second problem is that you have to specify the fully qualified name of the class that you want to run. This depends on the package that you have defined in the Java source file.
So something like this should work: (I'm assuming your class is in the default package, i.e. no package)
java -classpath c:\my_project\dir_prog\class_files ProgAudioJ
Can you provide the contents of the source file?
If your code and command is correct, I would suspect a delay, related to the asynchronous execution of the javac or to the filesystem.
Try adding a pause command in order to check if problem is the rapidity of execution of the commands or if the problem is in the command.

Categories