Visual Studio Code: Java code does not run at all - java

I am new to this world of programming.
I started like one week ago so I am a complete noob.
I have written some very basic java code but every time I press "run code" it spits out an error message.
It says "The command "javac" is either typed wrong or could not be found".
"exited with code=1 in 0.015 seconds"
What exactly is the problem and how do I solve it?
My code is free of typos and nothing is shown as wrong or incomplete.
I have tried to correct the java path in settings but that did not change anything at all.

The message is clear: VS code does not find javac.
It needs javac to compile your java source code.
As a start, see if you can do that manually: open a console, "cd" into the directory with the source code, and run javac WhateverClassYouHave.java to see if that works.
You might have for example only installed a JRE, not a JDK. Then you have java (the Java virtual machine), but not javac, the java compiler.

Related

Javac will not run despite Java being installed

While I have some experience in Python and JavaScript, I am new to Java and am trying it out for the first time. To get started, I went to http://www.java.com, downloaded the dmg there, and then used the package it gave me to install Java. After doing so, I received confirmation that Java had been installed successfully and I closed and trashed the dmg and package. Afterwards, I hopped on my terminal (I am using a Mac running on MacOS Monterey) and tried to use javac on a script I wrote. I received the following error message:
The operation couldn’t be completed. Unable to locate a Java Runtime that supports javac.
Please visit http://www.java.com for information on installing Java.
I went online and did some typical troubleshooting searches. First, I found a site suggesting that I create an environment variable $JAVA_HOME and set it equal to $(/usr/libexec/java_home) in .zshenv (yes, I use zsh not bash). I followed this instruction and when I run echo $JAVA_HOME I get /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home. However, running javac still did not work.
Once more, I went online and this time, I found the suggestion of adding javac to $PATH. So I went into .zshenv and added usr/bin/javac to $PATH (the $PATH export line now looks like this: export PATH="/Users/[redacted]/Library/Python/3.8/bin:/usr/bin/javac:$PATH"). This alteration was confirmed when I exited an reentered terminal and ran echo $PATH. However, once again, running javac yielded the same error.
I feel as if I am facing what must be a pretty common and easily fixable issue, but yet, I haven't yet been able to find a solution that works despite perusal of several other StackOverflow posts and tech articles. Still, I know I'm probably missing something simple, and if this is a duplicate of another question, please link that question in a comment, and I'll take this one down.
Thanks!

JAVA : Program Works in Terminal , But not in Visual Studio Code : Mac OS

My JAVA Programs used to run without any errors on both the Terminal and Visual Studio Code's Terminal .
After my Mac updated to MacOs Big Sur Version 11.0.1 , my JAVA programs show an error , even though the same code worked properly before .
There was no error before.
I checked my JDK and JRE Installations also
My Python files execute normally in the VS Code environment , so I do not think there is a problem with VS Code .
I have tried searching for an solution and have tried to clear the logs.
I would appreciate if anyone could guide me :)
Run the .java file in Terminal to check if JDK can work normally:
javac Hey.java
java Hey
If there's nothing wrong with the above command execution, turn to VS Code, uninstall Java Extension Pack and also delete the related folders under User/name/.vscode/extensions, then reinstall it again;
Set java.home and java.configuration.runtimes in User Settings.json.
Reference: Configure JDK.
Then run the project again to see if the problem goes away.
Install Java by searching for extensions in VSCode and try to run again.It will be executed. I belive it is not installed
You need to install the java plugin in VS Code.
Hi just found a solution here!
Initially guess it should be a problem with zsh.
so first open your terminal and type
open ~/.zshrc
at this point you will see text editor pops up with a zsh config file.
Add
export JAVA_HOME=`/usr/libexec/java_home
to the last line in that file, and save the file quit.
Then just reopen the VSC and you will notice everything would back to normal as usual.
I faced a similar problem. When running the same Java program, it worked in terminal but VS Code couldn't detect java.
All it turned out to be was I forgot to add Code itself to Path after reinstallation, nothing to do with Java.

Setting up Java compiler on Windows laptop

I have a laptop with a Windows 7 OS 64-bit. I'm trying to set up the Java compiler so when I type in "javac" in the command prompt, it will compile the Java file I want. I've had it done on my laptop before but someone did it for me. They went to the Environment Variables under the Advanced System settings, then edited the "PATH" so that it would recognized the compiler. So I did the exact same thing, I copied the file location of javac from ProgramFiles, and put it in PATH. It still does not compile my Java files. It says:
"javac" is not recognized as an internal or external command.
And before you ask, I did search through other forums on this site to see if there was an answer that can help. There are similar situations to mine but the solutions did not help me.
As of now, this is the file path that I tried copying into PATH:
C:\Program Files\Java\jdk1.7.0_25\bin This leads to a file marked javac. So far I'm still unsuccessful with setting up the compiler.
It looks correct. Did you restart the Command prompt every time you changed the PATH variable ? Also there's supposed to be only one path variable, so you're supposed to edit the existing one, entries are separated by a semi-colon (;).

Please help me get the java command to work

I'm learning how to make and run Java programs in Windows with Notepad and the command line. Right now I'm getting a well-known error when I try to run any sort of variant of java Main, Main being the starting class of the program, and I've done some research on it. The error is java.lang.NoClassDefFoundError, but any of the normal solutions for this error don't seem to be helping.
The program is in <root>\com\zork, and the classes are Main, Dungeon, and DungeonMaster, each in the package com.zork. They've all compiled just fine with the javac command, but attempting to run the program with java Main in either <root>\com\zork or just <root> keeps giving me the error above. I've tried many things with -cp, -classpath, and where I'm running java from. The current classpath is
<this directory doesn't matter>;.;"C:\Users\intprof\Desktop\ZORK in Java",
although the last entry has also been C:\Users\intprof\Desktop\ZORK in Java\com\zork, nonexistent, and a couple of other things.
What am I doing wrong? Thanks!
You need to run java com.zork.Main.

Javac can't find .class files, have right classpath

Although I'm not new to programming in general, I am new to java, and it seems to work a little bit differently. When trying to compile a .java with javac it gives me this error: cannot find symbol. I looked the cause of this up, and the error occurs when the .class file that you are referencing could not be found. I ensured that all of my references were spelled correctly, I ensured I had the right classpath, I tried compiling a .java file I know is valid, even on an XP computer just in case my Vista installation was the cause, but to no avail. Here are the contents of the .bat I am using to run javac, it could be the cause of my problems.
#echo off
"C:\Program Files (x86)\Java\jdk1.6.0_21\bin\javac" -encoding UTF8 -cp . hl.java
pause
Thank you for the help, this has been troubling me for quite some time now.
Can you show us this?
I tried compiling a .java file I know is valid
Could it be that you are using classes not compiled on . ? Unless your code is as simple as hello world, I'd say chances are high.
This is a long shot, but looking at your script and the error message you described, you're probably not running javac at all. Try this on the command line and see what it says:
C:\Program Files (x86)\Java\jdk1.6.0_21\bin\javac -version
If that doesn't work, then you're not pointing to a valid javac.

Categories