Since 2 days now I am trying to run the simplest program and I can't.
I run my programs from Windows cmd prompt.
Program:
public class Bla {
public static void main(String [] args) {
System.out.println("works");
}
}
Saved the source code as Bla.java.
Compiled the program with javac Bla.java --> Bla.class created. Tried to run the program with java Bla.class
I get the error:
"Could not find or load main class Bla.class"
I am not a complete newbie with java
1. I have configured my path and my classpath variable (Exact values below). Path: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\wbem;C:\Python27;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Skype\Phone;C:\mysql\bin;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI\wbin;C:\Program Files\Java\jdk1.8.0_144\bin
CLASSPATH: .;C:\Program Files\Java\jre1.8.0_144\lib;
2. my program doesn't belong to any package and doesn't call any package
I call everything from my command line. I tried uninstalling and reinstalling java developer kit.Every time I get the same error. This is not the first time I have installed java or run a program in java but I haven't written something in a long time. What am I doing wrong?
Thanks
Assuming your working directory containes Bla.class, and your CLASSPATH contains a "." (among others), java should be able to find it.
However, you should call it without ".class", like this: java Bla. Otherwise, java will think you want to call a class called "class" in package "Bla".
Related
I am trying to learn Java and I've made my first program and compiled it into a class file (the file is called aye.java and when compiled I have aye.class, I think the compilation worked). However when I use the java command in the folder where the class is located it just returns below error -
Could not find or load main class aye.class.
I have tried including the package name (com.java24hours) but it still doesn't work.. please help!
Commands I have tried:
java aye.class
java com.java24hours.aye.class
java aye
java com.java24hours.aye
program code:
package com.java24hours;
class aye {
public static void main(String[] args) {
//java code yeet
String aye = "Hello World!";
System.out.println(aye);
}
}
(I am running Linux on a Chromebook and have installed Java via the ppa:webupd8team/java)
Thanks.
I suppose you wanna put binaries to ./bin folder.
Compile aye.java:
javac -d ./bin aye.java
Then cd to ./bin directory and run the program:
cd bin
java com.java24hours.aye
well im stupid
since im new to java, i didn't know anything about packages and such. turns out all i had to do was put the class file in a folder named "ya" (that's the name of the package - i updated the program) and run the command
java -cp /home/ramsey/Documents/ya aye
(-cp stands for classpath, and you use it when you want to specify where you class is located MAKE SURE TO PUT IT IN A FOLDER NAMED AFTER YOUR PACKAGE!!!)
the wiki page is helpful: https://en.wikipedia.org/wiki/Classpath_(Java)
its under the section setting the path to execute java programs
thanks for the help everyone!
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
I have a set of instructions to create a Java application that takes multiple arguments when running the application from a CMD line.
The instructions state:
Thus, using the example set of above, assuming the main() method of your program is in a class called JavaClassName, the output should be:
$ java JavaClassName 4 7 file.csv
program output here
My question is:
Isn't this skipping the compile process?
Would they assume that loading the Java classes onto a computer that has never run this application before (or a directory with purely the .java files needed to run); running the cmd
$ java JavaClassName 4 7 file.csv
would output something?
Sidenote: Currently, running that CMD outputs
Error: Could not find or load main class JavaClassName
Have ran through multiple SO questions and online tutorials trying to get this to even run but I have yet to get it to work.
You ask:
Isn't this skipping the compile process?
Absolutely yes. A command line like java JavaClassName 4 7 file.csv assumes that there is a compiled class file "JavaClassName.class" in the current directory (or in some other directory or Zip/Jar file found in the CLASSPATH environment variable). And yes, to produce that "JavaClassName.class" class file, you have to use a java compiler first.
from Java 10 it is possible to run java programs that fit a single file without manually run the compiler first.
It will be compiled on the fly before execution. Nice and useful for scripting.
e.g. writing HelloWorld.java file
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
we can run it as
java HelloWorld.java
We can also add more classes in the single file.
from Java 11 we can also use shebang files
we have to specify which version of the java language you want to use
we have to save the file without .java extension
remember to give executable permissions chmod +x HelloWorld
writing HelloWorld file
#!/path/to/java --source 11
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
and we can run it as
./HelloWorld
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 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.