Running Java in Visual Studio Code gives error "not a statement" - java

I'm trying to run Java in visual studio code but it gives me this error, it works in IntelliJ though
EDIT:
Okay turns out I would need to make a Main.class file inside of the same file of the Main.java file, with that now it works properly in vscode. But I saw a friend of mine just making one new .java file and it works directly, how can I make vscode to be like so?
Main.java:
public class Main {
public static void main(String[] args){
System.out.println("Hello World");
}
}
Error:
[Running] cd "/Users/username/Documents/Java Projects/" && javac Main.java && java Main
Main.java:3: error: not a statement
System
^
Main.java:3: error: ';' expected
System
^
2 errors
[Done] exited with code=1 in 0.62 seconds
I tried reinstalling Java in Visual Studio Code to no avail
Update: I think I found the problem: Thing is, I tried out Java first with IntelliJ and before that I'd need to make a Java Project. It works through IntelliJ with that new Project file, when I run said Java program in vscode it works as well, but as soon as I make a new file outside of the project file, or a new file outside of the project folder, it gives out a new error which is this:
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main

javac main.java && java Main into your visual studio code terminal
If the error is the same
then set the temporary environment variable and try javac main.java && java Main again
Here is my powershell output
PS C:\Users\jilliss> javac Main.java ; java Main
Hello World
PS C:\Users\jilliss> java -version
openjdk version "17.0.6" 2023-01-17
OpenJDK Runtime Environment Temurin-17.0.6+10 (build 17.0.6+10)
OpenJDK 64-Bit Server VM Temurin-17.0.6+10 (build 17.0.6+10, mixed mode, sharing)
PS C:\Users\jilliss> cat .\Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
PS C:\Users\jilliss>
PS C:\Users\jilliss> $ENV:JAVA_HOME
D:\win\Scoop\apps\temurin17-jdk\current
PS C:\Users\jilliss> $ENV:CLASS_PATH
PS C:\Users\jilliss> $ENV:CLASSPATH
PS C:\Users\jilliss>
As you can see, I only set JAVA_HOME but everything works fine, probably because of your jdk version

Do not use Code Runner to execute scripts. Please use the Extension Pack for Java.
It will be easier to follow the official documentation to get started.

Related

'java class name' command not working, is there any change in recent version?

If I remember correctly
javac filename.java -> compile and generates classname.class(es)
java classname without .class extension
But when I try java filename.java executes successfully while java classname command gives the following error ,
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
java -version
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" in the terminal window.
System.out.println("Hello, World");
}
}
javap HelloWorld.class is giving below output
Compiled from "HelloWorld.java"
public class HelloWorld {
public HelloWorld();
public static void main(java.lang.String[]);
}
java HelloWorld.java -> executes fine, no class file generated.
java HelloWorld -> didn't execute.
Any idea why the program is behaving like this?
After some help from some stackoverflow veterans in the comment section I was able to understand what went wrong.
The latest version of Java have introduced launching single file source code directly using Java command.
from oracle docs.
To launch a single source-file program:
java [options] source-file [args ...]
To run Helloworld.java, you can directly call execute java Helloworld.java it will execute the java program and gives output without generating .class file in the current directory.
Why old way of running java class file didn't work for me?
I had a class path variable 'CLASSPATH' in my environment, so when I execute java HelloWorld it is not looking for class in current directory. Give java -cp . to explicitly give current directory to classpath.
java -cp . HelloWorld
Credits: Jon Skeet,Joachim Sauer, rzwitserloot

Error: Could not find or load main class in Textpad 8

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

Java can't find or load main

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.

Java install - Doesn't run the classes

I'm getting started with java development. So, I installed JRE and JDK in my computer.
Then, I created a simple Example.java file and saved it in my Desktop.
In the prompt, I executed
javac Example.java
and it worked ok. A .class file has been created in my Desktop.
Then, I tried to run the class, executing this:
java Example
and I got an error in a window alert, with this message:
Java Installation Not Completed
Unable to install Java
There are error in the command line switches: "Example";.
Check that the commands are valid and try again
Then, for testing, I executed both commands:
javac -version and
java -version. Both are installed in my computer.
What am I doing wrong?
I am running Windows 8 and have already set my environment variables.
Example.java:
public class Example {
public static void main(String args[]) {
System.out.println("Finally Java");
}
}
Try to remove installation again, look for all leftovers and remove them manually, if you changed the directory you installed java to, remove environment variables as well and set them again. You should also make registry cleanup: https://java.com/en/download/help/manual_regedit.xml Make installation through offline installer.

UnsupportedClassVersion error while trying to run simple Java code

I'm trying to compile my programs using the command prompt on Windows 7. I'm having a problem when I during compile. I created a test program:
class test
{
public static void main (String args[])
{
System.out.println("this is working!!!!!!");
}
}
I use the following instructions from the command line:
cd \
cd summer
Javac test.java
java test
I have even tried using javaw and I get the same message but in a pop-up box
Could not find the main class, program will exit.
Any suggestions?
It seems your JDK and JVM have different editions.
You are using different versions of JDK. Check the versions on your javac vs java. This error is telling you that your java and compiled class are not same versions. Check your path for configuration, type "set" in dos to see details.
Example: If you compiled your class with javac (version 7) and execute it with java (version 6)

Categories