Skip to content

Commit

Permalink
fixed changing CWD for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mbway committed Oct 7, 2016
1 parent 6c6c691 commit d497726
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/main/java/simulizer/Simulizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import simulizer.utils.UIUtils;

import java.nio.file.Path;
import java.nio.file.Paths;

public class Simulizer {
private static Image icon = null;
public static CommandLineArguments.Mode mode; // CMD_MODE || GUI_MODE

public static void main(String[] args) {
Path jarPath = FileUtils.getJarPath();
if(jarPath.toString().contains(".jar")) { // false if running from gradle or IDE
String jarPath = FileUtils.getJarPath();
if(jarPath != null && jarPath.contains(".jar")) { // false if running from gradle or IDE
// should be /some/path/Simulizer/lib/Simulizer-VER.jar
//
// first parent() to get lib/, second to get Simulizer/
String simulizerRoot = jarPath.getParent().getParent().toString();
String simulizerRoot = Paths.get(jarPath).getParent().getParent().toString();
System.out.println("moving current working directory to " + simulizerRoot);
FileUtils.setCWD(simulizerRoot);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/simulizer/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private Settings(File json, JsonObject jsonObject) {

settings.add(new ObjectSetting("editor", "Editor")
.add(new StringSetting("font-family", "Font family", "Font family (optional). Supports all installed monospace fonts, use single quotes for names with spaces. Separate multiple choices with commas", "monospace"))
.add(new IntegerSetting("font-size", "Font size", "Font size in px", 20, 0, Integer.MAX_VALUE))
.add(new IntegerSetting("font-size", "Font size", "Font size in px", 18, 0, Integer.MAX_VALUE))
.add(new StringSetting("initial-file", "Initial file", "Path to a file to load at startup (optional)"))
.add(new DoubleSetting("scroll-speed", "Scroll speed", "", 0.05, 0, 10))
.add(new BooleanSetting("soft-tabs", "Soft tabs", "", true))
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/simulizer/ui/windows/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ public Editor() {
engine = view.getEngine();
engine.setJavaScriptEnabled(true);

engine.setOnError((errorEvent) -> UIUtils.showErrorDialog("Editor JS Error", errorEvent.toString()));
// these error messages seem useless because they only warn about re-using web cache directories
// which means that opening multiple instances of Simulizer gives error messages because both are trying to access
// ~/.simulizer.GuiMode$App and ~/.simulizer.Simulizer
//engine.setOnError((errorEvent) -> UIUtils.showErrorDialog("Editor JS Error", errorEvent.toString()));

// making it so calling alert() from javascript outputs to the console
engine.setOnAlert((event) -> System.out.println("javascript alert: " + event.getData()));
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/simulizer/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.awt.*;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
Expand Down Expand Up @@ -162,8 +163,16 @@ public static String getResourceContent(String path) {
return sb.toString();
}

public static Path getJarPath() {
return Paths.get(Simulizer.class.getProtectionDomain().getCodeSource().getLocation().getPath());
public static String getJarPath() {
try {
// the location is given as file:/some/place which is OK on *nix because
// that gets translated to /some/place which is what we want.
// but on Windows the location gets translated to /C:/some/place which is not
// correct. So use File, given the URL to convert from URL to OS specific path
return new File(Simulizer.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath();
} catch (URISyntaxException e) {
return null;
}
}

public static void setCWD(String path) {
Expand Down

0 comments on commit d497726

Please sign in to comment.