Skip to content

Commit 4712224

Browse files
committed
fix "could not run" msg, other modernizing
1 parent 21c87ce commit 4712224

File tree

1 file changed

+68
-88
lines changed

1 file changed

+68
-88
lines changed

java/src/processing/mode/java/runner/Runner.java

Lines changed: 68 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,11 @@ public Runner(JavaBuild build, RunnerListener listener) throws SketchException {
116116
sketchErr.println(library.getName() + " does not run on this architecture: " + variant);
117117
int opposite = (bits == 32) ? 64 : 32;
118118
if (Platform.isMacOS()) {
119-
//if (library.supportsArch(PConstants.MACOSX, opposite)) { // should always be true
120119
throw new SketchException("To use " + library.getName() + ", " +
121120
"switch to " + opposite + "-bit mode in Preferences.");
122-
//}
123121
} else {
124122
throw new SketchException(library.getName() + " is only compatible " +
125123
"with the " + opposite + "-bit download of Processing.");
126-
//throw new SketchException(library.getName() + " does not run in " + bits + "-bit mode.");
127-
// "To use this library, switch to 32-bit mode in Preferences." (OS X)
128-
// "To use this library, you must use the 32-bit version of Processing."
129124
}
130125
}
131126
}
@@ -317,12 +312,12 @@ protected StringList getMachineParams() {
317312
//params.add("-Xrunhprof:cpu=samples"); // old-style profiler
318313

319314
// TODO change this to use run.args = true, run.args.0, run.args.1, etc.
320-
// so that spaces can be included in the arg names
315+
// so that spaces can be included in the arg names
321316
String options = Preferences.get("run.options");
322317
if (options.length() > 0) {
323-
String pieces[] = PApplet.split(options, ' ');
324-
for (int i = 0; i < pieces.length; i++) {
325-
String p = pieces[i].trim();
318+
String[] pieces = PApplet.split(options, ' ');
319+
for (String piece : pieces) {
320+
String p = piece.trim();
326321
if (p.length() > 0) {
327322
params.append(p);
328323
}
@@ -427,7 +422,7 @@ protected StringList getSketchParams(boolean present, String[] args) {
427422
GraphicsDevice[] devices = ge.getScreenDevices();
428423

429424
// Make sure the display set in Preferences actually exists
430-
GraphicsDevice runDevice = editorDevice;
425+
GraphicsDevice runDevice;
431426
if (runDisplay > 0 && runDisplay <= devices.length) {
432427
runDevice = devices[runDisplay-1];
433428
} else {
@@ -518,54 +513,52 @@ protected StringList getSketchParams(boolean present, String[] args) {
518513

519514

520515
protected void launchJava(final String[] args) {
521-
new Thread(new Runnable() {
522-
public void run() {
516+
new Thread(() -> {
523517
// PApplet.println("java starting");
524-
vmReturnedError = false;
525-
process = PApplet.exec(args);
526-
try {
518+
vmReturnedError = false;
519+
process = PApplet.exec(args);
520+
try {
527521
// PApplet.println("java waiting");
528-
int result = process.waitFor();
522+
int result = process.waitFor();
529523
// PApplet.println("java done waiting");
530-
if (result != 0) {
531-
String[] errorStrings = PApplet.loadStrings(process.getErrorStream());
532-
String[] inputStrings = PApplet.loadStrings(process.getInputStream());
524+
if (result != 0) {
525+
String[] errorStrings = PApplet.loadStrings(process.getErrorStream());
526+
String[] inputStrings = PApplet.loadStrings(process.getInputStream());
533527

534528
// PApplet.println("launchJava stderr:");
535529
// PApplet.println(errorStrings);
536530
// PApplet.println("launchJava stdout:");
537-
PApplet.printArray(inputStrings);
538-
539-
if (errorStrings != null && errorStrings.length > 1) {
540-
if (errorStrings[0].indexOf("Invalid maximum heap size") != -1) {
541-
Messages.showWarning("Way Too High",
542-
"Please lower the value for \u201Cmaximum available memory\u201D in the\n" +
543-
"Preferences window. For more information, read Help \u2192 Troubleshooting.", null);
544-
} else {
545-
for (String err : errorStrings) {
546-
sketchErr.println(err);
547-
}
548-
sketchErr.println("Using startup command: " + PApplet.join(args, " "));
549-
}
531+
PApplet.printArray(inputStrings);
532+
533+
if (errorStrings != null && errorStrings.length > 1) {
534+
if (errorStrings[0].contains("Invalid maximum heap size")) {
535+
Messages.showWarning("Way Too High",
536+
"Please lower the value for \u201Cmaximum available memory\u201D in the\n" +
537+
"Preferences window. For more information, read Help \u2192 Troubleshooting.", null);
550538
} else {
551-
//exc.printStackTrace();
552-
sketchErr.println("Could not run the sketch (Target VM failed to initialize).");
553-
if (Preferences.getBoolean("run.options.memory")) {
554-
// Only mention this if they've even altered the memory setup
555-
sketchErr.println("Make sure that you haven't set the maximum available memory too high.");
539+
for (String err : errorStrings) {
540+
sketchErr.println(err);
556541
}
557-
sketchErr.println("For more information, read revisions.txt and Help \u2192 Troubleshooting.");
542+
sketchErr.println("Using startup command: " + PApplet.join(args, " "));
558543
}
559-
// changing this to separate editor and listener [091124]
560-
//if (editor != null) {
561-
listener.statusError("Could not run the sketch.");
562-
vmReturnedError = true;
563-
//}
564-
// return null;
544+
} else {
545+
//exc.printStackTrace();
546+
sketchErr.println("Could not run the sketch (Target VM failed to initialize).");
547+
if (Preferences.getBoolean("run.options.memory")) {
548+
// Only mention this if they've even altered the memory setup
549+
sketchErr.println("Make sure that you haven't set the maximum available memory too high.");
550+
}
551+
sketchErr.println("For more information, read Help \u2192 Troubleshooting.");
565552
}
566-
} catch (InterruptedException e) {
567-
e.printStackTrace();
553+
// changing this to separate editor and listener [091124]
554+
//if (editor != null) {
555+
listener.statusError("Could not run the sketch.");
556+
vmReturnedError = true;
557+
//}
558+
// return null;
568559
}
560+
} catch (InterruptedException e) {
561+
e.printStackTrace();
569562
}
570563
}).start();
571564
}
@@ -604,40 +597,38 @@ protected void generateTrace() {
604597
return;
605598
}
606599

607-
Thread eventThread = new Thread() {
608-
public void run() {
609-
try {
610-
boolean connected = true;
611-
while (connected) {
612-
EventQueue eventQueue = vm.eventQueue();
613-
// remove() blocks until event(s) available
614-
EventSet eventSet = eventQueue.remove();
600+
Thread eventThread = new Thread(() -> {
601+
try {
602+
boolean connected = true;
603+
while (connected) {
604+
EventQueue eventQueue = vm.eventQueue();
605+
// remove() blocks until event(s) available
606+
EventSet eventSet = eventQueue.remove();
615607
// listener.vmEvent(eventSet);
616608

617-
for (Event event : eventSet) {
609+
for (Event event : eventSet) {
618610
// System.out.println("EventThread.handleEvent -> " + event);
619-
if (event instanceof VMStartEvent) {
620-
vm.resume();
621-
} else if (event instanceof ExceptionEvent) {
611+
if (event instanceof VMStartEvent) {
612+
vm.resume();
613+
} else if (event instanceof ExceptionEvent) {
622614
// for (ThreadReference thread : vm.allThreads()) {
623615
// System.out.println("thread : " + thread);
624616
//// thread.suspend();
625617
// }
626-
exceptionEvent((ExceptionEvent) event);
627-
} else if (event instanceof VMDisconnectEvent) {
628-
connected = false;
629-
}
618+
exceptionEvent((ExceptionEvent) event);
619+
} else if (event instanceof VMDisconnectEvent) {
620+
connected = false;
630621
}
631622
}
623+
}
632624
// } catch (VMDisconnectedException e) {
633625
// Logger.getLogger(VMEventReader.class.getName()).log(Level.INFO, "VMEventReader quit on VM disconnect");
634-
} catch (Exception e) {
635-
System.err.println("crashed in event thread due to " + e.getMessage());
626+
} catch (Exception e) {
627+
System.err.println("crashed in event thread due to " + e.getMessage());
636628
// Logger.getLogger(VMEventReader.class.getName()).log(Level.SEVERE, "VMEventReader quit", e);
637-
e.printStackTrace();
638-
}
629+
e.printStackTrace();
639630
}
640-
};
631+
});
641632
eventThread.start();
642633

643634

@@ -667,9 +658,7 @@ public void run() {
667658
// or the user manually closes the sketch window.
668659
// TODO this should be handled better, should it not?
669660
if (editor != null) {
670-
java.awt.EventQueue.invokeLater(() -> {
671-
editor.onRunnerExiting(Runner.this);
672-
});
661+
java.awt.EventQueue.invokeLater(() -> editor.onRunnerExiting(Runner.this));
673662
}
674663
} catch (InterruptedException exc) {
675664
// we don't interrupt
@@ -689,13 +678,7 @@ protected Connector findConnector(String connectorName) {
689678
// System.out.println("connector name is " + connector.name());
690679
// }
691680

692-
for (Object c : connectors) {
693-
Connector connector = (Connector) c;
694-
// System.out.println(connector.name());
695-
// }
696-
// Iterator iter = connectors.iterator();
697-
// while (iter.hasNext()) {
698-
// Connector connector = (Connector)iter.next();
681+
for (Connector connector : connectors) {
699682
if (connector.name().equals(connectorName)) {
700683
return connector;
701684
}
@@ -736,9 +719,7 @@ public void exceptionEvent(ExceptionEvent event) {
736719
handleCommonErrors(exceptionName, message, listener, sketchErr);
737720

738721
if (editor != null) {
739-
java.awt.EventQueue.invokeLater(() -> {
740-
editor.onRunnerExiting(Runner.this);
741-
});
722+
java.awt.EventQueue.invokeLater(() -> editor.onRunnerExiting(Runner.this));
742723
}
743724
}
744725

@@ -832,8 +813,7 @@ protected SketchException findException(String message, ObjectReference or, Thre
832813
for (StackFrame frame : frames) {
833814
try {
834815
Location location = frame.location();
835-
String filename = null;
836-
filename = location.sourceName();
816+
String filename = location.sourceName();
837817
int lineNumber = location.lineNumber() - 1;
838818
SketchException rex =
839819
build.placeException(message, filename, lineNumber);
@@ -857,7 +837,7 @@ protected SketchException findException(String message, ObjectReference or, Thre
857837
} catch (Exception e) {
858838
// stack overflows seem to trip in frame.location() above
859839
// ignore this case so that the actual error gets reported to the user
860-
if ("StackOverflowError".equals(message) == false) {
840+
if (!"StackOverflowError".equals(message)) {
861841
e.printStackTrace(sketchErr);
862842
}
863843
}
@@ -866,15 +846,15 @@ protected SketchException findException(String message, ObjectReference or, Thre
866846
try {
867847
// assume object reference is Throwable, get stack trace
868848
Method method = ((ClassType) or.referenceType()).concreteMethodByName("getStackTrace", "()[Ljava/lang/StackTraceElement;");
869-
ArrayReference result = (ArrayReference) or.invokeMethod(thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED);
849+
ArrayReference result = (ArrayReference) or.invokeMethod(thread, method, new ArrayList<>(), ObjectReference.INVOKE_SINGLE_THREADED);
870850
// iterate through stack frames and pull filename and line number for each
871851
for (Value val: result.getValues()) {
872852
ObjectReference ref = (ObjectReference)val;
873853
method = ((ClassType) ref.referenceType()).concreteMethodByName("getFileName", "()Ljava/lang/String;");
874-
StringReference strref = (StringReference) ref.invokeMethod(thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED);
854+
StringReference strref = (StringReference) ref.invokeMethod(thread, method, new ArrayList<>(), ObjectReference.INVOKE_SINGLE_THREADED);
875855
String filename = strref == null ? "Unknown Source" : strref.value();
876856
method = ((ClassType) ref.referenceType()).concreteMethodByName("getLineNumber", "()I");
877-
IntegerValue intval = (IntegerValue) ref.invokeMethod(thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED);
857+
IntegerValue intval = (IntegerValue) ref.invokeMethod(thread, method, new ArrayList<>(), ObjectReference.INVOKE_SINGLE_THREADED);
878858
int lineNumber = intval.intValue() - 1;
879859
SketchException rex =
880860
build.placeException(message, filename, lineNumber);
@@ -888,12 +868,12 @@ protected SketchException findException(String message, ObjectReference or, Thre
888868
// Implemented for 2.0b9, writes a stack trace when there's an internal error inside core.
889869
method = ((ClassType) or.referenceType()).concreteMethodByName("printStackTrace", "()V");
890870
// System.err.println("got method " + method);
891-
or.invokeMethod(thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED);
871+
or.invokeMethod(thread, method, new ArrayList<>(), ObjectReference.INVOKE_SINGLE_THREADED);
892872

893873
} catch (Exception e) {
894874
// stack overflows will make the exception handling above trip again
895875
// ignore this case so that the actual error gets reported to the user
896-
if ("StackOverflowError".equals(message) == false) {
876+
if (!"StackOverflowError".equals(message)) {
897877
e.printStackTrace(sketchErr);
898878
}
899879
}

0 commit comments

Comments
 (0)