2727import java .io .OutputStream ;
2828import java .nio .charset .Charset ;
2929import java .nio .file .Files ;
30+ import java .nio .file .Path ;
3031import java .nio .file .Paths ;
32+ import java .nio .file .StandardCopyOption ;
3133import java .lang .reflect .Type ;
3234import java .util .HashMap ;
3335import java .util .List ;
@@ -62,7 +64,8 @@ public class CadoodleUpdater {
6264 private Label binary ; // Value injected by FXMLLoader
6365 @ FXML // fx:id="currentVersion"
6466 private Label currentVersion ; // Value injected by FXMLLoader
65-
67+ @ FXML // fx:id="currentVersion"
68+ private Label infoBar ;
6669 @ FXML // fx:id="yesButton"
6770 private Button yesButton ; // Value injected by FXMLLoader
6871
@@ -96,9 +99,11 @@ void onYes(ActionEvent event) {
9699 System .out .println ("Yes path" );
97100 yesButton .setDisable (true );
98101 noButton .setDisable (true );
102+ infoBar .setText ("Downloading CaDoodle JAR..." );
99103 new Thread (() -> {
100104
101105 try {
106+
102107 String downloadURL2 = downloadJarURL ;
103108 System .out .println ("Downloading " +downloadJarURL );
104109 URL url = new URL (downloadURL2 );
@@ -114,10 +119,13 @@ public void process(double percent) {
114119 }
115120 });
116121 File folder = new File (bindir + latestVersionString + "/" );
117- File exe = new File (bindir + latestVersionString + "/" + jarName );
122+ File exe = new File (bindir + latestVersionString + "/" + jarName +"_TMP" );
123+ File exeFinal = new File (bindir + latestVersionString + "/" + jarName );
118124
119- if (!folder .exists () || !exe .exists () || sizeOfJar != exe .length ()) {
125+ if (!folder .exists () || !exeFinal .exists () || sizeOfJar != exeFinal .length ()) {
120126 folder .mkdirs ();
127+ if (exe .exists ())
128+ exe .delete ();
121129 exe .createNewFile ();
122130 byte dataBuffer [] = new byte [1024 ];
123131 int bytesRead ;
@@ -129,8 +137,12 @@ public void process(double percent) {
129137 pis .close ();
130138
131139 }
132- if (folder .exists () && exe .exists () && sizeOfJar == exe .length ())
140+
141+ if (exe .exists ())
142+ Files .move (exe .toPath (), exeFinal .toPath (), StandardCopyOption .REPLACE_EXISTING );
143+ if (folder .exists () && exeFinal .exists () && sizeOfJar == exeFinal .length ())
133144 myVersionString = latestVersionString ;
145+
134146 } catch (Exception e1 ) {
135147 // TODO Auto-generated catch block
136148 e1 .printStackTrace ();
@@ -151,7 +163,7 @@ public void launchApplication() {
151163 new Thread (() -> {
152164 String command ;
153165 try {
154- command = JvmManager .getCommandString (project , repoName , myVersionString ,downloadJsonURL ,sizeOfJson ,progress ,bindir );
166+ command = JvmManager .getCommandString (project , repoName , myVersionString ,downloadJsonURL ,sizeOfJson ,progress ,bindir , infoBar );
155167 } catch (Exception e ) {
156168 // TODO Auto-generated catch block
157169 e .printStackTrace ();
@@ -198,7 +210,22 @@ public void launchApplication() {
198210 System .out .println ("Running:\n \n " +finalCommand +"\n \n " );
199211 new Thread (() -> {
200212 try {
201- Process process = Runtime .getRuntime ().exec (finalCommand );
213+ // Get the current environment
214+ Map <String , String > env = new HashMap <>(System .getenv ());
215+
216+ // Extract JAVA_HOME from the JVM path
217+ // Assuming your command starts with the full path to java executable
218+ String javaHome = extractJavaHomeFromCommand (command );
219+ if (javaHome != null ) {
220+ env .put ("JAVA_HOME" , javaHome );
221+ }
222+
223+ // Convert environment map to array format
224+ String [] envArray = env .entrySet ().stream ()
225+ .map (entry -> entry .getKey () + "=" + entry .getValue ())
226+ .toArray (String []::new );
227+ // Execute with modified environment
228+ Process process = Runtime .getRuntime ().exec (finalCommand , envArray );
202229 Thread thread = new Thread (()->{
203230 BufferedReader reader = new BufferedReader (new InputStreamReader (process .getErrorStream ()));
204231 String line ;
@@ -257,7 +284,31 @@ public void launchApplication() {
257284 }).start ();
258285 }).start ();
259286 }
260-
287+ private static String extractJavaHomeFromCommand (String command ) {
288+ try {
289+ // Split the command to get the java executable path
290+ String [] parts = command .split (" " );
291+ if (parts .length > 0 ) {
292+ String javaPath = parts [0 ];
293+
294+ // Remove quotes if present
295+ javaPath = javaPath .replace ("\" " , "" );
296+
297+ // Get the parent directory of the bin folder
298+ Path path = Paths .get (javaPath );
299+ if (path .getFileName ().toString ().startsWith ("java" )) {
300+ // Go up from java executable to bin, then to JAVA_HOME
301+ Path binDir = path .getParent ();
302+ if (binDir != null && binDir .getFileName ().toString ().equals ("bin" )) {
303+ return binDir .getParent ().toString ();
304+ }
305+ }
306+ }
307+ } catch (Exception e ) {
308+ System .err .println ("Could not extract JAVA_HOME from command: " + e .getMessage ());
309+ }
310+ return null ;
311+ }
261312 public static boolean isWin () {
262313 return System .getProperty ("os.name" ).toLowerCase ().contains ("windows" );
263314 }
0 commit comments