Skip to content

Commit 3c2cac0

Browse files
committed
Add parameters to execute the java executable
1 parent 0273e9d commit 3c2cac0

6 files changed

Lines changed: 226 additions & 26 deletions

File tree

build.gradle.kts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sun.tools.jar.resources.jar
2+
13
plugins {
24
id("java")
35
id("java-library")
@@ -23,9 +25,15 @@ publishing {
2325
}
2426
}
2527

28+
tasks.jar {
29+
manifest {
30+
attributes["Main-Class"] = "fr.jachou.jvm.JavaVersionDownloader"
31+
}
32+
}
33+
2634

2735
group = "fr.jachou"
28-
version = "0.1.1"
36+
version = "0.1.2"
2937

3038
repositories {
3139
mavenCentral()

src/main/java/fr/jachou/jvm/JavaVersionDownloader.java

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package fr.jachou.jvm;
22

3-
import java.io.File;
4-
import java.io.IOException;
3+
import fr.jachou.jvm.managers.utils.JavaVersionList;
4+
import fr.jachou.jvm.ui.JVMGui;
5+
import fr.jachou.jvm.utils.CustomOutputStream;
6+
import fr.jachou.jvm.utils.Logger;
7+
8+
import javax.swing.*;
9+
import java.io.*;
510
import java.net.URL;
611
import java.nio.file.Files;
712
import java.nio.file.Path;
813
import java.nio.file.Paths;
914
import java.nio.file.StandardCopyOption;
15+
import java.util.Objects;
1016
import java.util.Scanner;
1117
import java.util.zip.ZipEntry;
1218
import java.util.zip.ZipInputStream;
@@ -18,7 +24,7 @@ public class JavaVersionDownloader {
1824
* Colors class
1925
* This class is used to color the text in the console
2026
*/
21-
static class Colors {
27+
public static class Colors {
2228
public static final String HEADER = "\033[95m";
2329
public static final String OKBLUE = "\033[94m";
2430
public static final String OKCYAN = "\033[96m";
@@ -36,29 +42,62 @@ static class Colors {
3642
*/
3743

3844
public static void main(String[] args) {
39-
boolean loop = true;
40-
41-
while (loop) {
42-
Scanner scanner = new Scanner(System.in);
43-
44-
// Message JVM = JavaVersionManager
45-
System.out.println(Colors.HEADER + " \n"
46-
+ "____________ ________ ___\n"
47-
+ "______ /__ | / /___ |/ /\n"
48-
+ "___ _ / __ | / / __ /|_/ /\n"
49-
+ "/ /_/ / __ |/ / _ / / / \n"
50-
+ "\\____/ _____/ /_/ /_/ \n"
51-
+ " \n");
52-
53-
try {
54-
System.out.print(Colors.OKCYAN + "Choose your version (only number) : ");
55-
int version = Integer.parseInt(scanner.nextLine());
56-
downloader(version);
57-
} catch (NumberFormatException e) {
58-
System.out.println(Colors.FAIL + "You must enter an integer!");
45+
46+
if (args != null) {
47+
if (Objects.equals(args[0], "-p") || Objects.equals(args[0], "--python")) {
48+
Logger.log("Python mod enabled!");
49+
50+
String pythonFile = "src/main/java/fr/jachou/jvm/py/java_dl.py";
51+
52+
try {
53+
Runtime runtime = Runtime.getRuntime();
54+
Process process = runtime.exec("python " + pythonFile);
55+
56+
// Lire la sortie du processus Python
57+
InputStream inputStream = process.getInputStream();
58+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
59+
String line;
60+
while ((line = reader.readLine()) != null) {
61+
System.out.println(line);
62+
}
63+
64+
// Attendre que le processus Python se termine
65+
int exitCode = process.waitFor();
66+
System.out.println("Le processus Python s'est terminé avec le code de sortie : " + exitCode);
67+
} catch (IOException | InterruptedException e) {
68+
e.printStackTrace();
69+
}
70+
} else if (Objects.equals(args[0], "-g") || Objects.equals(args[0], "--gui")) {
71+
Logger.log("GUI mod enabled!");
72+
73+
new JVMGui();
5974
}
75+
} else {
76+
boolean loop = true;
6077

61-
System.out.println();
78+
while (loop) {
79+
// Message JVM = JavaVersionManager
80+
System.out.println(Colors.HEADER + " \n"
81+
+ "____________ ________ ___\n"
82+
+ "______ /__ | / /___ |/ /\n"
83+
+ "___ _ / __ | / / __ /|_/ /\n"
84+
+ "/ /_/ / __ |/ / _ / / / \n"
85+
+ "\\____/ _____/ /_/ /_/ \n"
86+
+ " \n");
87+
88+
try {
89+
System.out.print(Colors.OKCYAN + "Choose your version (only number) : ");
90+
Scanner scanner = new Scanner(System.in);
91+
int version = Integer.parseInt(scanner.nextLine());
92+
if (version == 0) {
93+
Logger.log("Exiting...");
94+
System.exit(0);
95+
}
96+
downloader(version);
97+
} catch (NumberFormatException e) {
98+
System.out.println(Colors.FAIL + "You must enter an integer!");
99+
}
100+
}
62101
}
63102
}
64103

@@ -68,6 +107,16 @@ public static void main(String[] args) {
68107
*/
69108

70109
private static void downloader(int version) {
110+
try {
111+
String versionString = "Java_" + version;
112+
JavaVersionList.valueOf(versionString);
113+
} catch (IllegalArgumentException e) {
114+
System.out.println(Colors.FAIL + "[X] The version " + version + " does not exist!");
115+
return;
116+
}
117+
118+
Logger.logPass("The version " + version + " exists!");
119+
71120
System.out.print(Colors.BOLD + "[~] Checking if the link is good...");
72121
String url = String.format("https://chiss.fr/jvm/download/Java_%d.zip", version);
73122
try {
@@ -103,6 +152,7 @@ private static void downloader(int version) {
103152
Thread.sleep(1000);
104153
} catch (IOException e) {
105154
System.out.println(Colors.FAIL + "[X] The link does not exist!");
155+
e.printStackTrace();
106156
} catch (InterruptedException e) {
107157
e.printStackTrace();
108158
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package fr.jachou.jvm.ui;
2+
3+
import fr.jachou.jvm.JavaVersionDownloader;
4+
import fr.jachou.jvm.managers.utils.JavaVersionList;
5+
import fr.jachou.jvm.utils.Logger;
6+
7+
import javax.swing.*;
8+
import java.awt.*;
9+
import java.awt.event.ActionEvent;
10+
import java.awt.event.ActionListener;
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.net.URL;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
17+
import java.nio.file.StandardCopyOption;
18+
import java.util.zip.ZipEntry;
19+
import java.util.zip.ZipInputStream;
20+
21+
public class JVMGui extends JFrame {
22+
public JVMGui() {
23+
setTitle("Java Version Manager");
24+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
25+
setLayout(new FlowLayout());
26+
27+
JTextField textField = new JTextField("Press a button to download a JDK");
28+
textField.setEditable(false);
29+
textField.setPreferredSize(new Dimension(300, 30));
30+
add(textField);
31+
32+
// Créer les boutons pour chaque version de Java
33+
for (JavaVersionList version : JavaVersionList.values()) {
34+
JButton button = new JButton(version.toString().replace("_", " "));
35+
button.addActionListener(new ActionListener() {
36+
@Override
37+
public void actionPerformed(ActionEvent e) {
38+
// Appeler la méthode downloader avec la version correspondante
39+
downloader(version.ordinal() + 8);
40+
}
41+
});
42+
add(button);
43+
}
44+
45+
pack();
46+
setLocationRelativeTo(null);
47+
setVisible(true);
48+
}
49+
50+
private static void downloader(int version) {
51+
try {
52+
String versionString = "Java_" + version;
53+
JavaVersionList.valueOf(versionString);
54+
} catch (IllegalArgumentException e) {
55+
System.out.println(JavaVersionDownloader.Colors.FAIL + "[X] The version " + version + " does not exist!");
56+
return;
57+
}
58+
59+
Logger.logPass("The version " + version + " exists!");
60+
61+
System.out.print(JavaVersionDownloader.Colors.BOLD + "[~] Checking if the link is good...");
62+
String url = String.format("https://chiss.fr/jvm/download/Java_%d.zip", version);
63+
try {
64+
URL link = new URL(url);
65+
link.openStream().close();
66+
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The link is good!");
67+
Thread.sleep(1000);
68+
String path = System.getProperty("user.home") + "/.jdks";
69+
System.out.println(JavaVersionDownloader.Colors.OKCYAN + "The JDK " + version + " will be downloaded in the folder '" + path + "'");
70+
File directory = new File(path);
71+
if (!directory.exists()) {
72+
if (directory.mkdirs()) {
73+
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The folder has been created!");
74+
}
75+
} else {
76+
System.out.println(JavaVersionDownloader.Colors.WARNING + "[~] The folder already exists");
77+
}
78+
Thread.sleep(1000);
79+
System.out.print(JavaVersionDownloader.Colors.BOLD + "[~] Downloading...");
80+
Path destination = Paths.get(path + "/Java_" + version + ".zip");
81+
Files.copy(link.openStream(), destination, StandardCopyOption.REPLACE_EXISTING);
82+
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The JDK " + version + " has been downloaded!");
83+
Thread.sleep(1000);
84+
System.out.print(JavaVersionDownloader.Colors.BOLD + "[~] Unzipping...");
85+
unzip(destination.toString(), path + "/Java_" + version);
86+
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The JDK " + version + " has been unzipped!");
87+
Thread.sleep(1000);
88+
System.out.print(JavaVersionDownloader.Colors.BOLD + "[~] Deleting zip file...");
89+
Files.delete(destination);
90+
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The JDK " + version + " has been deleted!");
91+
Thread.sleep(1000);
92+
System.out.println(JavaVersionDownloader.Colors.OKGREEN + "[✓] The JDK " + version + " has been downloaded and installed!");
93+
Thread.sleep(1000);
94+
} catch (IOException e) {
95+
System.out.println(JavaVersionDownloader.Colors.FAIL + "[X] The link does not exist!");
96+
} catch (InterruptedException e) {
97+
e.printStackTrace();
98+
}
99+
}
100+
101+
private static void unzip(String zipFilePath, String destinationDir) throws IOException {
102+
File dir = new File(destinationDir);
103+
if (!dir.exists()) {
104+
dir.mkdirs();
105+
}
106+
byte[] buffer = new byte[1024];
107+
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(Paths.get(zipFilePath)))) {
108+
ZipEntry entry = zis.getNextEntry();
109+
while (entry != null) {
110+
String fileName = entry.getName();
111+
File newFile = new File(destinationDir + File.separator + fileName);
112+
if (entry.isDirectory()) {
113+
newFile.mkdirs();
114+
} else {
115+
new File(newFile.getParent()).mkdirs();
116+
Files.copy(zis, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
117+
}
118+
zis.closeEntry();
119+
entry = zis.getNextEntry();
120+
}
121+
}
122+
}
123+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package fr.jachou.jvm.utils;
2+
3+
import javax.swing.*;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
7+
public class CustomOutputStream extends OutputStream {
8+
private JTextArea textArea;
9+
10+
public CustomOutputStream(JTextArea textArea) {
11+
this.textArea = textArea;
12+
}
13+
14+
@Override
15+
public void write(int b) throws IOException {
16+
textArea.append(String.valueOf((char) b));
17+
textArea.setCaretPosition(textArea.getDocument().getLength());
18+
}
19+
}

src/main/java/fr/jachou/jvm/utils/Logger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class Logger {
44

5-
private static final String PREFIX = "[JVM]";
5+
private static final String PREFIX = "[JVM] ";
66

77
static class Colors {
88
public static final String HEADER = "\033[95m";

0 commit comments

Comments
 (0)