I'm a beginner at programming and practicing Java (using MacOS and jdk.17) but have been instructed to use JShell to deal with smaller/snippets of Java code. I installed JShell, opened a text editor to create and compile a file called Intro.jsh with the code:
System.out.println("Hello! Please enter some text");
String x = System.console().readLine();
System.out.println("You wrote " + x);
I then opened terminal to set my working directory to ensure the file is saved in the same environment as the directory. I opened JShell and used the following command:
jshell> --execution local Intro.jsh
I receive error messages such as:
Error:
';' expected
--execution local Intro.jsh
^
Error:
not a statement
--execution local Intro.jsh
However, when I used the command /open Intro.jsh it shows:
"Hello! Please enter some text.
Exception java.lang.NullPointerException: Cannot invoke "java.io.Console.readLine()" because the return value of "java.lang.System.console()" is null at (#2:1)
You wrotenull"
The first line works however the second requires an input from user. Whilst I can open the file, I'm struggling to solve why I cannot execute despite double checking my wd before opening JShell.
I believe there's something I'm definitely missing out so hopefully should be a quick fix but due to limited computing experience I've tried researching for many hours but cannot seem to find out why.
Please let me know any solutions to this and any help will be greatly appreciated :)
See the JavaDoc for System::console!
It says: "Returns the unique Console object associated with the current Java virtual machine, if any." And further down, for the return value: "The system console, if any, otherwise null."
So System::console returns null when invoked inside JShell. Seems that there is no console available for the JVM that executes JShell. And to be honest, that will not surprise me much, although I have not tried it before.
JShell itself is running on that console, having a second console would not make much sense in this context.
Related
As shown in the figure above; Every time you operate git command, some warning messages will appear.
I found some answers on the Internet;
For example, the following:
Only valid for git GUI; When I close the GIT GUI and reopen it, the warning message still exists;
I tried another way:I modified a configuration file in the GIT installation directory
I added a line of code: unset GIT_ TRACE_ PACKET GIT_ TRACE.
In this way, there will be no warning message through git GUI operation; However, when I operate through the console of idea, the warning message still exists and cannot be eliminated; May I ask you guys how to solve this problem?
This has nothing at all to do with java.
You get this message because you have GIT_TRACE set in your environment, to a value that Git does not understand. Your scripts as written cannot change your shell's setting. See environment-variables, and, e.g., Best way to set environment variables in calling shell and Can a shell script set environment variables of the calling shell?.
To unset GIT_TRACE in your current shell, run unset GIT_TRACE. To set it to a different variable, run export GIT_TRACE=value. The set of valid value-s includes 0 (off—this has the same effect as un-setting the variable), 1 and 2 (on and trace goes to stderr), higher numeric values (trace goes to an already-open file descriptor), and path names starting with /.
Any new shells you create may get GIT_TRACE set based on dot-files (e.g., .bashrc) or other startup items you have chosen. To change this, find your startup items and modify them. If your GUI creates a new shell every time you ask it to do anything, you will have to use this method.
the console won't run the program. nothing i do works.
even when i'm trying to run the most simple program it still won't work and it always gives me the same error.
1. a pop up error window saying "Cannot connect to VM - Socket closed"
2. java.lang.module.FindException: Module guy not found
what can i do to solve the problem?
You first have to fix all compile errors to be able to launch it.
Currently, the code can not be compiled because try is a keyword in Java and therefore cannot be used as a variable name.
I'm just starting out with Scala and have been following its Getting Started instructions. The second part of the instruction involves pulling the hello-world template by running the sbt new scala/hello-world.g8 command.
My problem is that it keeps on giving me this error:
Error: Could not find or load main class Anne
Caused by: java.lang.ClassNotFoundException: Anne
I'd like to know the reason for this, as well as any possible fix I might use.
The following is my insight and attempts on fixing this problem.
Insight:
1. I might have a problem with java installation/scala/sbt installation that needs this class Anne since even with other sbt commands like sbt sbtVersion I get the same error
Attempts:
1. Change command to retrieve from full url:
sbt new https://github.com/scala/hello-world.g8
2.(Edit): Previously I thought the repository scala/hello-world.g8 did not exist and tried getting from other repositories with no luck and with the same error as above. However it was pointed out below that the repository actually exists in this url https://github.com/scala/hello-world.g8, thanks Dmytro Mitin.
I was looking into the incorrect account (sbt) instead of (scala)
(Edit): Day 2
3. Uninstall/Reinstall sbt -- still getting the error
4. Checked if java running properly by compiling sample code and running ( successful )
I was actually running the command in Visual Studio Code's bash terminal. I tried running it with cmd and everything's now working fine.
This question is related to a previous question, here: Java Runtime.getRuntime().exec() appears to be overwriting $PATH
I am trying to build Go from source from inside a Java program. I can build it properly using Terminal, but Java's Runtime.getRuntime().exec() gets interesting results. I tried using ProcessBuilder, but could not get it to properly make Go. Using my current setup with exec(), it makes properly, but then fails two tests. Code snippet:
String[] envp = new String[4];
envp[0] = "CC=/usr/bin/clang";
envp[1] = "GOROOT_BOOTSTRAP=/usr/local/go";
envp[2] = "CGO_ENABLED=0";
envp[3] = "PATH=" + System.getenv().get("PATH");
Runtime.getRuntime().exec("./all.bash", envp, "$HOME/Desktop/go/src");
It runs properly and compiles properly, but when it gets to running the test suite, I get two errors:
--- FAIL: TestCurrent (0.00s)
user_test.go:24: Current: user: Current not implemented on darwin/amd64 (got &user.User{Uid:"502", Gid:"20", Username:"", Name:"", HomeDir:""})
FAIL
FAIL os/user 0.009s
and a much longer one that I won't paste here due to absurd length, but it comes down to:
panic: test timed out after 3m0s
...
FAIL runtime 180.056s
I haven't any idea why the former is failing, but for the runtime when I build from the Terminal, it says:
ok runtime 19.096s
So something is causing that to take absurd amounts of time. I did some googling, and heard that it might be fixed if I use ARM=5 as an environment variable, but that didn't change anything. Does anyone have any idea why these tests are failing when I build from Java as opposed to the Terminal?
Looking at the source code for the os/user package, it looks like the native user handling depends on having cgo enabled. If cgo=0 (your case), it will fall back to the USER and HOME environment variables.
source code in question
Try putting USER=whatever in your exec environment.
I'm afraid I'd need more information to diagnose the runtime issue.
So, here's my problem. I've got my ANTLR4 code successfully compiled, without errors and now I want to test it out. The ANTLR4 Documentation tells me, to test my applications, I shall do this:
java org.antlr.v4.runtime.misc.TestRig
I've tried this and got following error:
Error: Main Class org.antlr.v4.runtime.misc.TestRig couldn't be found or load.
I've checked if my CLASSPATH wasn't set, but everything was correctly set as it should be. I also tried moving the file directly to my test folder and opened CMD there and tried it again, I occur the same error. Searching in the Internet didn't help, as no one seemed to have occurred this error with ANTLR4 before.
Specs:
Java 1.7.0.55
ANTLR 4.4
There seems to be something wrong with your classpath, contrary to your belief everything is okay.
When I download the ANTLR 4 JAR and run TestRig:
wget http://www.antlr.org/download/antlr-4.4-complete.jar
...
java -cp antlr-4.4-complete.jar org.antlr.v4.runtime.misc.TestRig
I see the following on my console:
java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName
[-tokens] [-tree] [-gui] [-ps file.ps] [-encoding encodingname]
[-trace] [-diagnostics] [-SLL]
[input-filename(s)]
Use startRuleName='tokens' if GrammarName is a lexer grammar.
Omitting input-filename makes rig read from stdin.