Using command prompt to execute .java - java

I am trying to use the command line to execute a java class but I am receiving the below error
"error could not find or load main class
caused by java.lang.noclassdeffounderror"
I could used the "javac" to create my java class but then I ma getting this error.
Thank you for any help,
Regards
package start;
public class sdz1 {
public static void main(String[] args) {
System.out.println("Hello World !");
}
}
I just found that is not working when I am in the src folder created by eclipse where my ".java" is located.
Anyone has a explanation why is not working in this case ?

Check you've all required classes present at runtime as those were available at compile time
NoClassDefFoundError :- comes when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time
Read more: https://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html#ixzz6VZaZBT8y

Check that you have written all the "public class..." and the main method. If you then compile it, it shouldn't give you errors.

If your program is in directory start, execute this from the directory just above that to compile:
javac start/*.java
and then execute this to run:
java start/sdz1

Related

Java file compile using javac unable to refer to other files in the same directory

I am trying to migrate from IDEs like Eclipse to a standalone Java environment, but I'm having problems tying together multiple files into a project.
Here is some sample code, where both files are in the same directory:
App.java
package com.example.main;
public class App {
public static void main(String[] args) {
Example.test();
}
}
Example.java
package com.example.main;
public class Example {
public static void test() {
System.out.println("It's working");
}
}
When running App.java in an IDE, the expected output of It's working is printed, however after executing javac *.java the files seem to ignore eachother.
Here is the error that occurs when executing java App.java after it's been compiled:
App.java:5: error: cannot find symbol
Example.test();
^
symbol: variable Example
location: class App
1 error
error: compilation failed
How can I compile the files in a project so that they recognise eachother?
If you running Java 11 and above, java App.java will compile App.java only.
If you need to refer Example.java, first you need to compile both java files into a directory.
Let give it named 'classes'. The command will be
javac -d classes *.java
After that, you can run it via
java -cp classes com.example.main.App. Please note that App is without .class suffix
Of course, it is advisable to use build tools like Apache Maven or Gradle to build your project if it grow larger or need other dependencies.

Java.lang.NoClassDefFoundError in command prompt run [duplicate]

This question already has answers here:
NoClassDefFoundError: wrong name
(8 answers)
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 2 years ago.
I'm trying to run my code in command prompt and it gives me error .. Can anyone know whats wrong on it?
Error: Could not find or load main class hello
Caused by: java.lang.NoClassDefFoundError: FirstQuarter/hello (wrong name: hello)
This type of error is due to the class not found in the Classpath during runtime but found during compile time.
Look to print 
System.getproperty("java.classpath")
which will print the classpath so you get an idea as to the actual runtime classpath.
Also, make sure you pass the fully qualified name of the class to "java" command which contains the main method for execution.
directory_that_holds_package>java package_name.Class_name
First, I take a guess that your program could run smoothly in Eclipse and Idea, but it gives this error in command line.
Now, you should include your program's package in command line. If your program is like:
package firstprogram;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
Then you should run java firstprogram.HelloWorld in FirstQuarter folder.
This error is mainly due to when the program is not able to access the class which you have defined in your program and it can be due to reasons like you have not defined the proper classpath or you have not included the required library needed to run that class. The reasons can be many.
So try to run your code on any IDE as you will be able to easily identify the errors.

Unable to run hadoop aplication due to NoClassDefFoundError

I am new in Hadoop and I have been following some tutorials like this. I have found a nice set of mapreduce examples in here. I was able to run wordcount example but I am not able to run the EnhancedTopN example. It gives me the error: Exception in thread "main" java.lang.NoClassDefFoundError: EnhancedTopN (wrong name: samples/topn_enhanced/EnhancedTopN). I have correctly compiled the java file, although it gives me a note saying "Note: EnhancedTopN.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.". What could be wrong?
Thanks
NoClassDefFoundError with the message "wrong name" means that the package structure of your class is wrong, or that you are running it the wrong way. This has nothing to do with Hadoop, just with the way packages work in Java.
Is your class EnhancedTopN in a package named samples.topn_enhanced; do you have the following package statement at the top of the source file?
package samples.topn_enhanced;
public class EnhancedTopN {
// ...
}
The directory structure of your project must match the package structure, so the source file EnhancedTopN.java should be in a directory samples\topn_enhanced, and you must compile and run it from the base directory of the package structure:
C:\Project> javac samples\topn_enhanced\EnhancedTopN.java
C:\Project> java samples.topn_enhanced.EnhancedTopN
Going into the directory and using java EnhancedTopN will not work and will give you the error that you are asking about:
C:\Project\samples\topn_enhanced> java EnhancedTopN -- error!
See: Lesson: Packages in Oracle's Java Tutorials.

Calling dynamic library method - IntelliJ IDEA project - Java

I have the following Java native function
private static native void fillEnergyArrays(double currentTemp, double concNA, double concMG);
The function is implemented in C. The code has been tested and I have created a working dynamic library, libhyfiModel.jnilib, which I can use to call the method from a Java file, Temp.java. There are no problems in this case when I run the program with the one file. The method call works and returns the expected value.
The problem:
I am taking Temp.java and libhyfiModel.jnilib and trying to import them into a large IntelliJ IDEA project which uses Maven as a build manager. I have put my libhyfiModel.jnilib file in my correct directory so it loaded when the program executes. I double checked that it's loaded using this code in Temp.java:
static {
try {
System.loadLibrary("hyfiModel");
}
catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load\n" + e);
}
}
No exception is thrown here, so libhyfiModel.jnilib is found and loaded correctly, but when the native method is executed I get the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: ca.virology.baseByBase.DiffEditor.fillEnergyArrays(DDD)V
at ca.virology.baseByBase.DiffEditor.fillEnergyArrays(Native Method)
at ca.virology.baseByBase.DiffEditor.main(DiffEditor.java:266)
I have looked at the documentation here https://www.jetbrains.com/idea/help/configuring-module-dependencies-and-libraries.html and followed the instructions with no success.
So the dynamic library is being loaded and I have added the directory containing libhyfiModel.jnilib as a dependency but the method can not execute... What am I missing?
EDIT:
I created a new IntelliJ project with just Temp.java and I am able to call the native function with no errors, so I'm thinking there must be a problem with my project configurations. Any IntelliJ experts out there?
Figured it out. It wasn't a configuration error since the library was being loaded properly. The problem was with Temp.java package membership used in the IntelliJ project.
I had a Temp.h file that was generated using the command javah -jni Temp that contains the C function definitions for my native functions.
I didn't realize that the native function definitions in Temp.h are different when the Java file Temp.java has a specified package.
I found my answer at Another JNI, C++, DLL, UnsatisfiedLinkError <Native Method>
It is shown that the header files' function names change with package membership.

QtJambi example not executing

I'm trying to compile and run the tooltip code from this tutorial. I obtained QtJambi from my package manager (the package is qtjambi-beta from AUR), which installed it into the directory /opt/qtjambi-beta/. In particular, the qtjambi-4.7.0.jar file is located at /opt/qtjambi-beta/qtjambi-linux64-community-4.7.0/qtjambi-4.7.0.jar.
Now, I made a folder called qtpractice and put the example in there under the name JambiApp.java. The code I put into it was exactly as follows (following the example I linked):
package qtpractice;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QWidget;
public class JambiApp extends QWidget {
public JambiApp() {
setWindowTitle("Tooltip");
setToolTip("This is QWidget");
resize(250, 150);
move(300, 300);
show();
}
public static void main(String args[]){
QApplication.initialize(args);
new JambiApp();
QApplication.exec();
}
}
I compiled it with javac qtpractice/*.java -cp /opt/qtjambi-beta/qtjambi-linux64-community-4.7.0/qtjambi-4.7.0.jar, which worked fine. I then tried to execute it with java qtpractice.JambiApp, and I got the following error:
Error: Could not find or load main class qtpractice.JambiApp
EDIT: Based on some advice from the comments, I tried this command instead: java -cp /opt/qtjambi-beta/qtjambi-linux64-community-4.7.0/qtjambi-4.7.0.jar qtpractice.JambiApp
. When I did this, I got the following error again:
Error: Could not find or load main class qtpractice.JambiApp
What did I miss? From what I can tell, I did everything necessary to make it execute.
You need to include all jars Qt Jambi needs in classpath.
This can be done on CLI with command similar to
java -cp /opt/qtjambi-beta/qtjambi-linux64-community-4.7.0/qtjambi-4.7.0.jar:/opt/qtjambi-beta/qtjambi-linux64-community-4.7.0/qtjambi-linux64-gcc-4.7.0.jar:. qtpractice.JambiApp
When compiling, native jar does not need to be present, as the native libraries are just for Jambi classes to be able to use Qt.

Categories

Resources