Invalid java regex escape sequences not flagged in eclipse - java

I teach a Java-based Advanced Computer Science class and we primarily use the Eclipse IDE. I recently had an assignment where students explored regex in Java, and have run into an issue where incorrectly formed escape sequences actually pass in Eclipse, but fail java compilation when running through my test automation. Here is an example of a regex that should fail but does not on eclipse:
in = in.replaceAll(" ([^\s]*([^0-9\\+|\\-|\\/|\\(|\\)|\\*|\\.|\\s]+|[^0-9]\\.|\\.[^0-9])[^\s]*) "," Error:$1"+separator+" ");
When compiling outside of eclipse, this code correctly fails to compile with the following error message:
RegularExpressions.java:60: error: illegal escape character
in = in.replaceAll(" ([^\s]*([^0-9\\+|\\-|\\/|\\(|\\)|\\*|\\.|\\s]+|[^0-9]\\.|\\.[^0-9])[^\s]*) "," Error:$1"+separator+" ");
^
I have been unable to figure out why eclipse ignores this issue. I am assuming that there is some setting somewhere that I need to update, but I have not found it yet. It is particularly frustrating for students when their code "works for them" but then fails my testing.
Any help would be greatly appreciated.

\s is a valid Java escape for space in Java 15 and later (JLS ยง3.10.7)
There is a bug in Eclipse see here which accepts \s when the language level is less than 15. This will be fixed in 2021-12 (4.22)

Related

Cannot execute .jsh file on JShell

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.

NLTK unable to find java.exe (spontaneous path reduction)

Similar questions were posted here and here, and my question is actually based on what was suggested in answers to those questions.
I try to parse some German texts using Stanford Parser and NLTK.
from nltk.parse.stanford import StanfordParser
import os
os.environ['STANFORD_PARSER'] ='C:\PretestKorpus\stanford-parser-full-2018-10-17'
os.environ['STANFORD_MODELS'] = 'C:\PretestKorpus\stanford-parser-full-2018-10-17'
parser=StanfordParser(model_path="C:\PretestKorpus\germanPCFG.ser.gz")
new=list(parser.raw_parse("Es war einmal ein Bauer"))
Then, of course, I get NLTK was unable to find the java file! error:
So I set configurations like this:
nltk.internals.config_java('C:\Program Files (x86)\Java\jre1.8.0_251\bin\java.exe')
but it returns
NLTK was unable to find the C:\Program Files (x86)\Java\jre1.8.0_251in\java.exe file!
Use software specific configuration paramaters or set the JAVAHOME environment variable.
So, somehow Python reduces the path \\jre1.8.0_251\bin\java.exe to \\jre1.8.0_251in\java.exe
Looks like this:
Setting environment variable does not help either (returns NLTK was unable to find the java file!error). Obviously, python does not read the path correctly. But for what reason and how to fix that? Any help will be appreciated.
In python \b inside a String is resolved to a backspace character. Therefore you see the white BS in the picture, becuase the console tries to represent this special character (BS for backspace).
What you need to do is to escape the \ inside your String like so
nltk.internals.config_java('C:\\Program Files (x86)\\Java\\jre1.8.0_251\\bin\\java.exe')
It is a good practice to alway escape all backslash characters, so you can be sure that problems like this one never occur.

Why couldn't "org.antlr.v4.runetime.misc.TestRig" not be found or load?

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.

Getting a TypeError: wrong argument type Array (expected Module) with include java_import

I'm just starting using JRuby and when I test following code saved as a script
include java_import
frame = javax.swing.JFrame.new
frame.getContentPane.add javax.swing.JLabel.new('Hello, World!')
frame.setDefaultCloseOperation javax.swing.JFrame::EXIT_ON_CLOSE
frame.pack
frame.set_visible true
I get the following error
Switch to inspect mode.
irb(main):001:0> include java_import
TypeError: wrong argument type Array (expected Module)
from org/jruby/RubyModule.java:2068:in `include'
from (irb):1:in `evaluate'
from org/jruby/RubyKernel.java:1066:in `eval'
from org/jruby/RubyKernel.java:1392:in `loop'
from org/jruby/RubyKernel.java:1174:in `catch'
from org/jruby/RubyKernel.java:1174:in `catch'
from C:\jruby-1.7.2\/bin/jirb_swing:54:in `(root)'
irb(main):002:0>
Can someone help me identify what I'm doing wrong? I have jruby-1.7.2 installed (C:\jruby-1.7.2), added to the PATH in system variables, and running on Windows 7. At the command prompt, I can test jruby and java version correctly
I also launched the jirb_swing via jruby(1.9.3).exe and at the prompt typed
irb(main):001:0> include java_import
and I get the same error message
Update
Hi All
After searching further on the Internet pages on JRuby and Java, I have resolved the problem successfully.
First the two ways that worked for me in calling Java from JRuby:
Using "include Java" (without the quotes in my script file and with capital J)
Using "require 'java'" (without the " quotes and notice the single quotes around java, now starting with lower-cap j)
The "java_import" I was using was an error on my part in being careless as I read the detailed document at GitHub (https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby)
I will research more about the differences or lack thereof in using the "include" vs. "require" statements and post back. As a real novice, this has been a real eye opener
Sri

Compiling PlayN Flash samples

I've been following this guide to get playn up and running in eclipse indigo. Hit some major bumps along the way (the largest being swithing to gwt 2.3) but I've managed to get it compiling the as files. The problem is when it starts to create the swf I get this flash compilation error:
C:\Users\ted\AppData\Local\Temp\1351877287607-0\testsgameflash.as(145): col: 220 Error: Incorrect number of arguments. Expected no more than 1.
function $replace_0(this$static,from,to){var regex;if(from<256){regex=toPowerOfTwoString(from);regex='\\x'+'00'.substring(regex.length)+regex}else{regex=String.fromCharCode(from)}return this$static.replace(RegExp(regex,'g'),String.fromCharCode(to))}
C:\Users\ted\AppData\Local\Temp\1351877287607-0\testsgameflash.as(146): col: 131 Error: Incorrect number of arguments. Expected no more than 1.
function $replaceAll(this$static,regex,replace){replace=__translateReplaceString(replace);return this$static.replace(RegExp(regex,'g'),replace)}
It looks like it is casting the value as 'RegExp' instead of creating a new instance of RegExp. I'm not sure where the mess up is and I just wondering what I can do to fix it and get everything compiled

Categories