Eclipse autocomplete broken between certain lines of code - java

I've been having issues with Eclipse's autocomplete for a while, as it stops working halfway through coding a project. After lots of unsuccessful debugging, I tried narrowing down my code and have the resulting bare-bones example of my file (minus lots of other functions):
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class main {
public static void main(String[] args) {
List<String> zlist = new ArrayList();
zlist.forEach(fileName -> { // CONTENT-ASSIST BREAKS HERE
String test = "";
if (true) { // CONTENT-ASSIST WORKS RIGHT AFTER THIS LINE
}
});
Iterator<String> iter = null;
while ("".equals("")){}
}
}
For some reason, none of the content-assist (Ctrl+Space) works between the forEach and the if statement. Commenting out either Iterator or while loop line makes it work again, but I need that current setup. The file runs fine without errors too. Is there a reason why this occurs, and is there a proper fix?
I have checked: Window > Preferences > Java > Editor > Content Assist > Advanced and Java Proposals is checked.
Booting up Eclipse in -clean mode didn't help, nor did cleaning or rebuilding the project.
The issue occurs even in a new workspace and when I create a new file with the same code (it being the only file in the project).
My setup is: Eclipse Java EE IDE for Web Developers, Neon.1 Release (4.6.1). Windows 7 64-bit.
Here's some screenshots of the errors:
The last screenshot shows it working when I comment out the Iterator declaration at the bottom. Not quite sure why that affects the autocomplete.

Related

JavaFX native packaging - bizarre error with initialising static variables

I'm working on a JavaFX project in Netbeans that's currently around 3000 lines long, and I've packaged as an .exe regularly for testing but never come across an issue like this curious incident.
When packaging natively as a Windows .exe file, I found that after installing the .exe and launching my program I was getting a popup saying "Class {mypackage}/{mymainclass} not found." followed by "Failed to launch JVM."
Launching the program in Netbeans, packaging as an .exe, and launching the .jar in Powershell with "java -jar {app}.jar" all gave absolutely no errors. Even the .jar inside the installed application folder was fine, with no errors on the command line.
After a few hours of trawling through git commits and packaging natively, I managed to trace the issue down to a single line of code:
private static ComboBox choices = new ComboBox();
When I initialise the ComboBox in an initialiser, the program magically works after being installed from an .exe:
private static ComboBox choices;
{
choices = new ComboBox();
}
However, when I use a static initialiser (by placing static in front of the first curly brace), even though Netbeans states "initialiser can be static", I get the same error as before.
This is puzzling, because Java is obviously fine with the code itself, but after it's been through the native packager it will not launch. I've used plenty of similar lines of code to initialise static variables in other classes, with no ill effects.
I tried adding a similar line to the main class: private static CheckBox chkbox = new CheckBox();
It caused the exact same error after the .exe was installed (as before, the .jar was fine), but when I cut-and-pasted the line to a different class, it had no effect.
Interestingly, my main class already has a static boolean that is initialised in the same way that the ComboBox and CheckBox were: private static boolean someBool = true; but the boolean causes no problems.
Can anyone explain the reason behind this? Thank you.
Edit: I also tried packaging on a different machine, but it stopped working after the same git commit.
I'm using version 1.8.0_144 of the JDK and JRE.
Edit 2: here is the main class of a simple example that produces the error.
package testproject;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class MainApplication extends Application {
private static CheckBox testCheckBox = new CheckBox(); // Causes error after launching from installed .exe
/* UNCOMMENT THIS SECTION AND REMOVE THE " = new CheckBox()" ABOVE TO FIX THE ERROR
{
testCheckBox = new CheckBox();
}
*/
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Empty window");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I would avoid creating JavaFX widgets in static initializers (such as a static field or static block), as this would create them when class is loaded, which might happen in any thread. You want that to happen in JavaFX Application Thread.
In your case it happens in main thread, which is definitely not correct.
Check this answer for more details about JavaFX application startup, and how threads are involved in the process.
Trying to initialize a JavaFX widget outside of "JavaFX Application Thread" is (to my understanding) undefined behavior, and might fail in many different ways (or succeed by luck). If that creation fails, then the class loading fails (because static init has failed), hence "class not found". Unfortunately, since this happens while loading main class, you don't get to see the initial error, just "class XXX not found".
Possibly in your case, the java launcher can deal with this initialization just fine, but the launcher used by your .exe is different. I'm not too surprised it fails. I'm equally surprised it works with regular java :)

Intellij IDEA cannot resolve 'andThen' functional interface method when referenced Function::andThen

How can I get IntellijIDEA to find the 'andThen' method of the core java 8 'Function' interface when using the notation Funciton::andThen? I've tried many things unsuccessfully.
My intellijIDEA module is configured to java 8, the sdk used is the oracle java 8, I've invalidated the caches, and tried several other things, but still the editor marks and then as: "cannot resolve method 'andThen'".
I can launch and build this sample, so I think it's something to do with the static code analyzer. Maybe a bug?
package foo.bar;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class Meh {
public static void main(String... args) {
final List<Function<String, String>> fs = new ArrayList<>();
fs.add(s -> s + "1");
fs.add(s -> s + "2");
final Function<String, String> f =
fs.stream()
//copmiles from cli and project->make, but editor shows: Cannot resolve method 'andThen'
.reduce(Function::andThen)
.get();
System.out.println(f.apply(""));//succesfully prints 12
final Function<String,String> f2 = f.andThen(s-> s+"a");
//succesfully prints 12a
System.out.println(f2.apply(""));
}
}
Something interesting is that when I reference f.andThen, the static code analyzer doesn't complain. It only happens when I reference Function::andThen.
This is not a problem when using eclipse. Or again, when compiling from the command line, or going to project -> make
This seems to be fixed in IntelliJ 15.0.2, with 15.0.1 I could reproduce this error marker.
Quite some bugs which sound like your kind of problem are mentioned in the release notes, section "Java.Error Highlighting", e.g.:
IDEA-146604 (Bug) Valid code highlighted as error (Enum::compareTo)
IDEA-147873 (Bug) Good code marked red with lambdas/method references

Eclipse Mars (4.5) hot swap code in debugger not working

So I updated to Eclipse Mars (4.5) and for some reason I'm unable to use the hot swap code in the debugger. Normally I could do something like this:
public static void main(String[] args){
while(true){
System.out.println("123");
}
}
Then if I started it in debug mode, changed the text to "321", then save, then it would update without the need for restarting it. It behaves exactly like it was run in "Run" mode instead of "Debug".
What I have tried:
Creating a new workspace, creating a fresh project, using the code above, nothing happens
Have several JDKs installed, have tried with java 6, 7 & 8, changed the workspace and/or the project settings to use the different JDKs, nothing happens (the fact that I have several versions of java installed shouldn't matter as it was just the moment I updated eclipse it stopped working)
Tried uninstalling removing any config files to eclipse (on a mac, so that would be every file/folder with the word "eclipse" in the ~/Library folder, ran a "find" search to detect all the files). Then tried to create a new workspace, now project, the code snipped, ran in debug mode, nothing happens on save.
Have also made sure I have "Auto Build" enabled, even tried to "clean" it, and disable auto build, then save the code, then do a manual build while the debugger was running: nothing happens
I'm starting to get desperate as I have a hard time getting work done without having debug mode available so any help/hints in the right direction would be of much appreciation.
HotSwap doesn't work with static methods. However it works fine with instance methods, so it will work on this code:
public class Main {
public static void main(String[] args) {
new Main().f();
}
public void f() {
while(true){
System.out.println("123");
}
}
}
Ok so I finally found the problem. It seems that you can't edit loops while they are running. Say you have a loop like this:
public static void main(String[] args){
while(true){
System.out.println("123");
}
}
Then you can't edit the "123" string.
You can how ever edit methods which are called inside the loop like this:
public static void main(String[] args){
while(true){
System.out.println(methodA());
}
}
public static String methodA(){
return "123";
}
Now you can edit the string "123" and it will update.
This also applies for infinite "for" loops, so guess the rule of thumb is that the method body has to be "re-called" before updating, and it isn't enough to wait for the next loop call.

ClassNotFoundException while Running Java Application

I am getting a ClassNotFOundException while running a code.I am using Eclipse.
Although it has main method with proper arguments it is still not able to launch the code.
Package name and import is also correct.
public class TestNew {
public static void main(String[] args)
{
ArrayList<String> sumList=new ArrayList<String>();
Cm cm=new Cm();
sumList=cm.sumListCombo();
for(int i=0;i<=sumList.size();i++)
{
System.out.println(sumList.get(i));
}
}
}
Import of respective packages are done.
Try refreshing the Project, then clean it and build it. If there is no problems, your code will work. If there is a problem, it will be some kind of 'Organize Imports' issue. Ctrl+Shift+o and you are ready.
Try Ctrl + Shift + O if you're on windows.
This organizes your imports(adds missing ones and removes unnecessary ones).
Then on the menu bar, Project >> Clean...
Then re-launch your app.

Weird error with Locale.getISOCountries()

I'm using this code:
for (final String code : Locale.getISOCountries())
{
//stuff here
}
But on compile I get this error:
[ERROR] Line 21: No source code is available for type java.util.Locale; did you forget to inherit a required module?
And then a stack trace of compiler errors.
I'm doing both of these imports at the beginning of the class:
package com.me.example;
import java.util.Locale;
import java.util.*;
What can be wrong?
In Netbeans i see the autocomplete options and no syntax error for the Locale object...
Something screwy with your setup, the folllowing program works fine for me.
import java.util.*;
import java.util.Locale;
public class Donors {
public static void main (String [] args) {
for (final String code : Locale.getISOCountries()) {
System.out.println (code);
}
}
}
The fact that it's asking for source code leads me to believe that it's trying to compile or run it in some sort of debugging mode. You shouldn't need the source code for java.util.* to compile, that's just bizarre.
See if my simple test program works in your environment, then try looking for something along those lines (debugging options). Final step: compile your code with the baseline javac (not NetBeans).
UPDATE:
Actually, I have found something. If you are creating GWT applications, I don't think java.util.Locale is available on the client side (only the server side). All of the references on the web to this error message point to GWT and its limitations on the client side which are, after all, converted to Javascript goodies, so cannot be expected to support the entire set of Java libraries.
This page here shows how to do i18n on GWT apps and there's no mention of java.util.Locale except on the server side.
Looks like there might be something fishy in your build environment, as Locale.getISOCountries() should work just fine. Try compiling a small test program manually and see if you get the same error.
Definitely try to boil this down to a minimum, three-line program (or so), compile from the command-line, then put that class into your IDE and see if you still get the error, and if not, then change/add one line at a time until you have the original failing program, looking for what causes the problem. I'm thinking maybe some other import in your code is importing a Locale class? Why in the world would it be looking for source code?
See what happens when you compile this from the command-line:
import java.util.*;
public class LocaleTest {
public static void main(String[] args) {
Locale.getISOCountries();
}
}

Categories

Resources