Using Java 8
I have set my PATH as 'C:\Program Files\Java\jdk1.8.0_144\bin'
I also tried to set my CLASSPATH as 'C:\Program Files\Java\jre1.8.0_144\lib\rt.jar', though I read it is not neccesary.
From Class01.java I have no problem creating Class01.class
javac Class01.java -> created Class01.class
Still, when I try to run program
java Class01
I got message
Error: Could not find or load main class Class01
If anyone know, how to fix this, I appreciate every hint.
Btw. My program does nothing but printing Hello world, if it has something to do with my problem.
You will need to tell Java about the classpath where it should find your file. I think you are missing the classpath parameter in your java command (see below). Here is a simple example how you could create a java file, compile and run it:
A. Create File:
public class X {
public static void main(String[] args) {
System.out.println("Blah!");
}
}
B. Compile:
"%JAVA_HOME%\bin\javac" X.java
C. Run it using the `-classpath` parameter:
"%JAVA_HOME%\bin\java" -classpath . X
This will print out:
Blah!
Note that JAVA_HOME is a System variable in Windows which needs to point to the location of your Java Runtime Environment.
Related
I'm getting the error Error: Could not find or load main class ExcelFileEditor when trying to run my program. I compiled the program with no errors by:
javac -cp C:\Users\rperera\IdeaProjects\LinkingNames\libs\*;. C:\Users\rperera\IdeaProjects\LinkingNames\src\ExcelFileEditor.java
I tried doing:
java ExcelFileEditor
java -cp C:\Users\rperera\IdeaProjects\LinkingNames\libs\* ExcelFileEditor
but I keep getting the same error. I'd really appreciate it if someone could help me fix this problem!
public static void main(String[] args) {
//this allows the Py4J module in Python to use whichever methods it needs from this class
ExcelFileEditor editor = new ExcelFileEditor(new File(args[0]));
GatewayServer server = new GatewayServer(editor);
server.start();
}
The package is excel.writer
If you are not specifying where to place the generated class files then its going to be in the same directory as the source file.
Try
java -classpath C:\Users\rperera\IdeaProjects\LinkingNames\libs\*;C:\Users\rperera\IdeaProjects\LinkingNames\src excel.writer.ExcelFileEditor
I am trying to run a java program in linux server which includes an imported jar.
While compiling it is not giving any issues , but when I try to run, it is giving an error "Error: Could not find or load main class ". If I remove the import and its references , my code is working fine without any load issues.
Any inputs please
javac -cp "/opt/CARKaim/sdk/pwdsdk.jar" SamplePwd.java //No issues
java -cp "/opt/CARKaim/sdk/pwdsdk.jar" SamplePwd //Error: Could not find or load main class
Assuming SamplePwd has an entry-point (e.g. main(String[] args)) make sure the current folder is also in your class-path, like
java -cp "/opt/CARKaim/sdk/pwdsdk.jar:." SamplePwd
I created the following class located in the MainJPrint.java file
import com.XXXXX.pdfPrint.PDFPrint;
public class MainJPrint
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
print(".....");
}
public static String print (final String url)
{
Object rc = AccessController.doPrivileged(new java.security.PrivilegedAction()
{
public Object run()
{
...
}
}
}
}
In the same folder I have a jar archive jPrint.jar
I compile the class using the following command
>javac -classpath jPrint.jar MainJPrint.java
When I'm trying to execute resulted class file, I get this error:
>java MainJPrint
java.lang.NoClassDefFoundError: com/XXXXX/pdfPrint/PDFPrint
If I uncomment the Hello World line and comment the next line, the program runs fine.
I'm using j2sdk1.4.2 installed at C:\j2sdk1.4.2.
I do also have installed other java versions (at C:\Program Files\Java: jre 1.6.0_01, jre 1.6.0_02, j2re1.4.2, jre6, jre7, jdk1.7.0_03)
The PATH variable contains the C:\j2sdk1.4.2\bin path, however I think the java.exe is loaded from the upper version, but it shouldn't matter and I can call it like
>C:\j2sdk1.4.2\bin\java.exe MainJPrint
jPrint.jar is a third party archive and I need to create an applet which exposes a method so I can call it with javascript. I'm not a java developer, I'm having some little troubles and I'm really on an end here.
I tried other options like:
>java MainJPrint -cp .
>java MainJPrint -cp jPrint.jar
So how can I execute that class file which uses a class located in a separate archive?
To execute a class that depends on external JARs, you need to specify all elements of the classpath on the command line.
If you don't specify a classpath, Java automatically uses . (the current directory), which is why, if MainJPrint didn't depend on jPrint.jar, your invocation java MainJPrint would have worked.
But when you specify -cp jPrint.jar, Java does NOT automatically add the current directory to the classpath, which means that it then cannot find MainJPrint. You need to specify both. On Mac/*nix, the following invocation should work:
java -cp jPrint.jar:. MainJPrint
Or on Windows:
java -cp jPrint.jar;. MainJPrint
OK, I just cannot get java to run my .class files:
I follow steps in Oracle tutorial and try to run this program:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Compiling OK:
PS C:\Users\Ztaz> javac .\HelloWorldApp.java
But after I try to run it, I get this:
PS C:\Users\Ztaz> java .\HelloWorldApp.class
Error: Could not find or load main class .\HelloWorldApp.class
no exception, nothing.
Here's my PATH variable, if it helps (split into lines, for readability):
%JBOSS_HOME%;
%SYSTEMROOT%;
%M2%;
%JAVA_HOME%\bin;
...
JAVA_HOME is set to "C:\Program Files\Java\jdk1.7.0". My question sounds a lot like this one but I had this problem on Java SE 6 as well, so I decided to post separate question.
Run it without the .class: java HelloWorldApp
This causes issues for lots of people starting out with Java. Not sure why Java doesn't just look for both files (the name provided and the name with .class appended).
I'm learning Java am having trouble running an example program.
I have two files:
GoodDog.java:
class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;
}
void bark() {
if (size > 60) {
System.out.println("Wooof! WoooF!");
} else if (size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}
GoodDogTestDrive.java:
class GoodDogTestDrive {
public static void main (String[] args) {
GoodDog one = new GoodDog();
one.setSize(70);
GoodDog two = new GoodDog();
two.setSize(8);
System.out.println("Dog one: " + one.getSize () );
System.out.println("Dog two: " + two.getSize () );
one.bark();
two.bark();
}
}
They are typed out exactly the way they are in the book and compile without issue. When I try to run GoodDogTestDrive I get this:
nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$ java GoodDogTestDrive.class
java.lang.NoClassDefFoundError: GoodDogTestDrive/class
Exception in thread "main" nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$
What am I doing wrong?
Don't include the .class in the command:
java GoodDogTestDrive
It is important to note that while the resolution was simple for this problem case (simply removing .class from the Java command line), java.lang.NoClassDefFoundError can be hard to pinpoint in the context of Java Web application developments.
This Java exception means that a “runtime” Java class could not be loaded and/or found in the current Thread context classloader. This is normally the results of:
Missing library in your runtime classpath.
Static {} block code initializer execution failure (preventing class loading of the affected class).
JAR file packaging / class loader tree problems such as mix of JAR files deployed at the child & parent class loader.
It is recommended for any Java beginner to properly understand this type of problem in anticipation of future troubleshooting episodes.
just run the program with "java yourClass".
Do not use .class in the end.
Try it and let us know.
If the GoodDog class file is not located at the current working directory you will need to set the classpath on your java command....
java GoodDogTestDrive -cp ./path/to/gooddog/
Issue-java.lang.NoClassDefFoundError
Root Cause: Incorrect Java path set in Environment Variable Section
Solution: Set correct JAVA_HOME Path
Steps->Environment Variable Setting (My Comp-Right Click ->Properties->Env Variable->Advance Tab ->Variable)
Create new JAVA_HOME Environment Variable.
JAVA_HOME **.;C:\Program Files (x86)\Java\jdk1.6.0_14**
Set JAVA_HOME variable in PATH Variable section.
PATH %JAVA_HOME%\bin
Set JAVA_HOME variable in CLASSPATH Variable
CLASSPATH %JAVA_HOME%\jre\lib
Restart System
Verify all variable
echo %CLASSPATH%
echo %JAVA_HOME%
echo %PATH%
Compile java class javac Test.java
Run Java program java Test
Thrown if the JVM or a ClassLoader instance tries to load in the definition of a class (method call or creating a new instance) and no definition of the class could be found.
The reason for NoClassDefFoundError is that a particular class is not available on runtime but was available during compile time.
1.The class belongs to a missing JAR or JAVA file or JAR was not added into classpath.
2.Class is not available in Java Classpath.
3.NoClassDefFoundError in Java due to Exception in Static Initializer block. when your class perform some static initialization in static
block, and if static block throw an Exception, the class which is
referring to this class will get NoclassDefFoundError in Java.
4.Your Classpath, PATH or JAVA_HOME is not setup properly or JDK installation is not correct. which can be resolved by re-installing JDK.
5.Do a maven clean-install and simply restart your server with necessary Source(.java) files.
I was facing the same problem as you.
I resolved the problem like this:
Check the class path in evironment variables
I opened the project in navigator of eclipse and I checked the class files. My class files were in different folders not in the bin folder. I just copied the missing files into the bin folder then the problem was resolved.
Simply copying the missing .class files to bin directory of eclipse project resolved the problem.