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?)
Related
I am trying to run a Java application which has many dependencies. In the past I have use the following command to launch the application
java -cp "program.jar:jar1.jar:jar2.jar:jar3.jar:[...]" program
However as the list of dependencies have grown, I have moved them into an arguments file, the contents of this file are:
-cp "\
program.jar:\
jar1.jar:\
jar2.jar:\
jar3.jar:\
[...]"
And I am running the application with
java #arguments-file program
Everything up to this point works fine.
Sometimes I end up with beta versions of program.jar, they share all of the same dependencies, but program.jar is renamed program-beta.jar.
So to run the jar the following command would be used
java -cp "program-beta.jar:jar1.jar:jar2.jar:jar3.jar:[...]" program
or more specifically, I would use an environment variable, so that the same script can be used, and the variable would be set to either program.jar, or program-beta.jar, depending on the circumstance
java -cp "$PROGRAM_JAR:jar1.jar:jar2.jar:jar3.jar:[...]" program
Now that I am using an arguments file I was hoping to be able to be able to do something like:
java -cp "$PROGRAM_JAR" #arguments-file program
However using -cp twice causes one of the two to be ignored, resulting in a java.lang.ClassNotFoundException exception.
Is there any way around this that allows me to specify one jar file by name, but abstract away all of the others so that my java command isn't thousands of characters?
This will be running entirely on Linux, so any command line "magic", such as using grep is fine, so long as the resulting code is easily readable
You could just write two bash scripts production.sh and beta.sh that contain a reference on program.jar and program-beta.jar, respectively.
Also, the classpath can contain wildcards (see man-page), so if you can ensure that on disk exists only one of the two versions, you can write it like this:
java -cp "program*:jar1.jar:jar2.jar:jar3.jar:[...]"
In the long term, you might think about building/running it with Maven or Gradle, depending on your requirements.
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.
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.
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
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.