The principal class XXXX has not been found
The code constructs perfectly but when I try to execute it I get the same error again.
I type the following "Hello world" code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hola mundo");
}
}
I get the same error.
It must be a problem of my program ,I use GEANY because my teacher told me that I can't use netbean or similar.
How do you compile and execute your code? Is it some IDE (Eclipse) or just command line?
IDE should take care about this issue. In command line you have to include all classes into the classpath. Sometime it is confusing because when you compile your code mutually is in classpath but you have to mention it litreally to run.
Here is example
javac Hello.java
compiles Hello.java source to the Hello.class code to the current location. Literally ./Hello.class
To run you should mention this location
java -classpath . Hello
without -classpath . java doesn't know where your class is.
Related
I've been trying for 1 week to run a simple java program using third party jar, but I keep getting error. Just now I try this program as below
import org.apache.commons.lang3.*;
public class HelloWorld{
public static void main(String[] args){
String x = "abcd";
System.out.println(StringUtils.capitalize(x));
}
}
and when I try to compile and run, command prompt return error as below
c:\training\java\exercise>javac -cp ".;./org/apache/commons/lang3/commons-lang3-3.9.jar" HelloWorld.java
HelloWorld.java:5: error: cannot find symbol
System.out.println(StringUtils.capitalize(x));
I take this example from this sites as for reference https://www.programcreek.com/2014/01/compile-and-run-java-in-command-line-with-external-jars/
can anyone help me how to solve this? Thanks in advance
enter image description here
There is an article about just that available here
I assume you're on Windows as the working folder has disk C: so it should be Windows.
Bottom line it looks like you're mixing the notions of folder and jar.
Make sure that the folder C:\training\java\exercise contains both HelloWorld.java and commons-lang3-3.9.jar
Also make sure you're in the C:\training\java\exercise folder
Then compile like this:
javac -cp ".;./commons-lang3-3.9.jar" HelloWorld.java
Now this should create HelloWorld.class file
Now Run the java process:
java -cp ".;./commons-lang3-3.9.jar" HelloWorld
I've been trying to get back into programming and I've been redoing some old labs. I'm setting up Textpad 8 so I can run java applications and it works fine until I add a package statement like so:
package pkg;
public class inPkg{
public static void main(String args[]){
System.out.println("Hello World");
}
}
The file's location: C:\214\pkg\inPkg.java
When I compile everything is fine but when I try to run it, I get this error message:
Error: Could not find or load main class inPkg
Tool completed with exit code 1
Compile Java Tool:
Parameters: javac -classpath "$FileDir;h:\214\;c:\214\;" $File
Run Java Application Tool:
Parameters: java -classpath "$FileDir;h:\214\;c:\214\;" $BaseName
These tools are the only thing I changed in the configuration. The classpath have been written to follow the lab. instructions.
PS. Without the packages statement, the application runs perfectly.
Because probably you are not using the fully qualified class name when you do the java run. use
java -classpath 'your class path' pkg.inPkg
It will compile and execute correctly with following commands
C:\214>javac.exe pkg\inPkg.java
C:\214>java.exe pkg.inPkg
Note that the file location is C:\214\pkg\inPkg.java, however you execute the commands from C:\214
Hello stackoverflow community!
I am in the beginning of my journey of becoming a programmer and currently in the process of learning Java. I have strictly been using Eclipse to compile my programs. However, when I try to run the program through the command line I get:
"Error: Could not find or load main class FirstProg."
I've read through some other discussions on the forum and experimented with different methods, but I cannot get it to execute the program.
The path to my program (FirstProg.java) is as follows: C:\Users\smj7v\workspace\LearningJava\src\com\smj\programmingByDoing
When I enter "javac FirstProg.java" in the CMD it compiles the program and I can see the FirstProg.class generated in the path folder, but when I try to execute, "java FirstProg," it throws the error.
I tried doing things like "java com.smj.programmingByDoing.FirstProg" along with other variations but so far nothing has worked. Obviously I am doing something wrong. Please help!
public class FirstProg {
public static void main(String[] args) {
System.out.println("Mr. Mitchell is cool.");
}
}
The program runs fine in Eclipse btw.
here's a sample way of doing
create following class MyTest.java under c:\com\test folder
package com.test;
public class MyTest
{
public static void main(String[] args)
{
System.out.println("test fle");
}
}
now when you are compiling make sure that you use option -d
run following
cd \com\test
javac -d . mytest.java
next from same folder (com\test),
java com.test.MyTest
Step 1: Write Java Program.
Step 2: Compile java file to class file and generate byte code.
Step 3: Byte code translate to machine code and run on JVM.
Steps to write, compile and run java program using command prompt.
(i). Save the program. After using a text editor, such as NotePad, to create your Java program, save the program with a .java extension.
(ii). Open Command Prompt.
(iii). Navigate to the correct folder.
(iv). Set path.
(v). Compile the program.
Example:javac JavaClassName.java
(vi). Run the program.
Example:java JavaClassName
Visit the good blog to read all steps with example and images:
https://javatutorialdetails.blogspot.in/2017/10/how-java-program-work-step-by-step-in.html
Run your class after setting classpath:
set classpath=%classpath%;.;
java com.smj.programmingByDoing.FirstProg
C:\Users\smj7v\workspace\LearningJava\src> javac com\smj\programmingByDoing\FirstProg.java
C:\Users\smj7v\workspace\LearningJava\src> set classpath=%classpath%;.;
C:\Users\smj7v\workspace\LearningJava\src> java com.smj.programmingByDoing.FirstProg
I copied a simple java app from a book, but am getting a missing class error.
This is the app:
# cat MyFirstApp.java
public class MyFirstApp {
public static void main (String[] args) {
System.out.println("I Rule!");
System.out.println("The World");
}
}
It compiles:
[root#dev hfjava]# javac MyFirstApp.java
[root#dev hfjava]#
But when I go to run the program I get this error:
[root#dev hfjava]# java MyFirstApp.java
Error: Could not find or load main class MyFirstApp.java
What am I doing wrong?
You're doing the javac MyFirstApp.java correctly, but the reason it is not working when you actually try to run the program is because you are typing:
java MyFirstApp.java
when all you have to type is:
java MyFirstApp
Just leave out the .java extension when you actually run the program. Once you do this, your program should execute flawlessly! And remember, as long as you are a programmer, you do rule the world!
Write a comment if this doesn't work and I will continue to try to help you.
Try java -cp . MyFirstApp as your command line.
Try running java MyFirstApp in your command line after running the javac command.
I have recently installed canopy (i don't know if this has anything to do with java)
i have set to system variables the path to javac and java
i was writing compiling and running java programs normally until now that it keeps telling me it can not find or load main class.class
example of code:
class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
what is happening?
ok i changed hello to Hello
i run at powershell:
javac Hello.java
java Hello.class ( i am in the directory of the file)
Error: Could not find or load main class Hello.class
You need to run your program using
java Hello
and omit the .class extension
That should compile fine which Java compiler are you using? Make sure to always save and compile your program before running it.