ããã«ã¡ã¯ãèã®ç©´ã©ãã®S.Aã§ãã
ä»åã¯Javaã§ä½æããã¢ããªã±ã¼ã·ã§ã³ãWindowsåãã«å®è¡å¯è½ãªç¶æ ã§é å¸ããæ¹æ³ã«ã¤ãã¦ãç´¹ä»ãã¾ãã è¿å¹´ã¢ããªã®ä¸»æµã¯ãWEBã¢ããªã«ãªãããã¹ã¯ãããã¢ããªãä½æããæ©ä¼ã¯æ¸ã£ã¦ãã¦ããã¨ã¯æãã¾ããã ãªãªã¼ã¹ãµã¤ã¯ã«ã®å¤åã§æ¥éã«å¤åããJavaã®ä¸çã§ããã¹ã¯ãããã¢ããªã®ãã«ãæ¹æ³ãæ§ã ã«å¤åãã¦ããããã åå¿ãå ¼ãã¦è¨äºã«ã¾ã¨ãããã¨æãã¾ãã
jarãã¡ã¤ã«ãä½ã
Javaã§å®è¡å¯è½ãªå½¢å¼ã¨è¨ãã°ãjarãã¡ã¤ã«ãããã¾ãã jarãã¡ã¤ã«ã®ä½ææ¹æ³ã¯ããã¤ãããã¾ãããGradle ã§ããã° java plugin ã® jar ã¿ã¹ã¯ã使ãã¾ãã
ä¾ãã°æ¬¡ã®ãããªãµã³ãã«ã¢ããªã±ã¼ã·ã§ã³ãããã¨ãã¾ãã (Hello World ã¨æ¸ãããã¦ã£ã³ãã¦ãåºãã ãã®ã¢ããªã±ã¼ã·ã§ã³)
package jp.example; import javax.swing.*; import java.awt.*; public class Application { private Application(){ JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.getContentPane().setPreferredSize(new Dimension(300, 300)); JLabel label = new JLabel("Hello world."); frame.add(label); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(Application::new); } }
build.gradle 㧠java ãã©ã°ã¤ã³ã追å ãã jar ã¿ã¹ã¯ã®è¨å®ã¨ã㦠Main-Class ã«å ã»ã©ã®ã¯ã©ã¹ãæå®ãã¾ãã jar ã¿ã¹ã¯ãå®è¡ããã°ã./build/lib é ä¸ã« jar ãä½æããã¾ãã
apply plugin: 'java' jar { manifest { attributes 'Main-Class': 'jp.example.Application' } }
jarã®å®è¡çµæã¯ãããªæãã§ãã
éçºè ã®ç°å¢ã§ã¯ãJDKãã¤ã³ã¹ãã¼ã«ããã¦ãããã jarãã¡ã¤ã«ããã®ã¾ã¾å®è¡ã§ãã¾ããã ä¸è¬ã¦ã¼ã¶ã«åºãã¢ããªã±ã¼ã·ã§ã³ãé å¸ããå ´åã¯ãjarãã¡ã¤ã«ã®ã¾ã¾ã ã¨ä¸èªç±ã ã£ãããã¾ãã Windows ç°å¢ã§åããã¨ãã¦ãä¾ã㰠次ã®ãããªåé¡ã«ç´é¢ãã¾ãã
ã»jar ãé¢é£ä»ãããã¦ããªãããã¯ãªãã¯ã§å®è¡ã§ããªã (exeã«ããå¿ è¦ããã)
ã»å®è¡ç°å¢(JRE)ããªãããåããªã
ã»ã¤ã³ã¹ãã¼ã«ããã¦ããJavaã®ãã¼ã¸ã§ã³ãéãããåããªã
éçºè ã§ããã°é£ãªã対å¦ã§ããããããã¾ãããã å®è¡ã«å¿ è¦ãªãã¡ã¤ã«ãå ¨ã¦å梱ããã¦ãã¦ãä½ãèããã¯ãªãã¯ã ãã§ããã«å®è¡ã§ãããããªå½¢ãçæ³ã§ãã
Javaã®ãã¼ã¸ã§ã³ã®è©±
åãJavaã¨ããã©ããã¼ã¸ã§ã³ã«ãã£ã¦å¯¾å¦æ¹æ³ãéã£ã¦ãããããJavaã®ãã¼ã¸ã§ã³ãã©ããªã£ã¦ãããå°ãæ¸ãã¦ããã¾ãã 2023å¹´2æç¾å¨ãJava SEã®ãã¼ã¸ã§ã³ã¯ 19ãææ°ã¨ãã¦ãªãªã¼ã¹ããã¦ãã¾ãã ãã ããJava 19 ã 18 ãªã©ã¯ãåå¹´å¾ã«ãªãªã¼ã¹ãããæ°ãã¼ã¸ã§ã³ã®ç»å ´ãæã£ã¦ãµãã¼ãçµäºã¨ãªããããæ°è¦ã®ããã¸ã§ã¯ãã§ã¯ãã¾ãé¸æãããªããã¼ã¸ã§ã³ã¨ãªã£ã¦ãã¾ãã 対ãã¦ãJava 8, 11, 17 ãªã©ã¯LTS(é·æãµãã¼ã)çã¨ãã¦ãæ°å¹´éãµãã¼ãããããã¼ã¸ã§ã³ã¨ãªã£ã¦ãã¾ãã
Java 8 ã®æ代ã§ã¯ãJavaã®ã¢ããªã±ã¼ã·ã§ã³ãå®è¡ããã«ã¯ã ã¦ã¼ã¶å´ãèªèº«ã®PCã«JRE(Javaå®è¡ç°å¢)ãã¤ã³ã¹ãã¼ã«ãã¦ããå¿ è¦ãããã¾ããã
ããããJava 9 ã§å°å ¥ããã Project Jigsaw ã§ã¢ã¸ã¥ã¼ã«ãåããããããã«ãªãã JREã®ä¸ããã¢ããªã±ã¼ã·ã§ã³å®è¡ã«å¿ è¦æä½éã®ã©ã¤ãã©ãªãæ½åºã§ããããã«ãªãã¨ç¶æ³ãå¤ããã¾ãã
LTSã§ãã Java 11 ããã¯ãJRE ãåç¬ã§é å¸ãããªããªãã¾ããã ããã¯ã¤ã¾ããJava 11 以éã§ä½ãããã¢ããªã§ã¯ãã¦ã¼ã¶ãèªåã®PCã«JREãã¤ã³ã¹ãã¼ã«ããã®ã§ã¯ãªãã ã¢ããªéçºè ãå¿ è¦æå°éã®JREãã¢ããªã«å梱ãããã¦ã¼ã¶å´ã¯JREãåå¥ã«ã¤ã³ã¹ãã¼ã«ãããã¨ãªãã¢ããªãå®è¡ã§ãããããªå½¢ã«æ¹å転æããã¨ãããã¨ã®ããã§ãã
Java 11 以éã§JREãå梱ãã
ã§ã¯ãJava 11以éã§å¿ è¦æå°éã®JREãå梱ãã Self-Contained Java Application ãä½ãã«ã¯ã©ã®ãããªæé ãå¿ è¦ãªã®ãã¨ããã¨ã â jar ãä½ã â¡ jdeps ã§ä¾åã¢ã¸ã¥ã¼ã«ã確èªãã ⢠jlink ã§æå°ã®JREãæ½åºãã ⣠launch4j ãªã©ã§ exe ãä½ã ã¨ãªãã¾ãã
jdeps ã使ã
jarã®ä½ãæ¹ã¯åè¿°ã®éããªã®ã§ãâ¡ãã確èªãã¾ãã jdeps ã jlink ã¯JDKã®ã¤ã³ã¹ãã¼ã«ãã£ã¬ã¯ããªä¸ã® bin ãã£ã¬ã¯ããªã«åå¨ããCLIã®ã³ãã³ãã§ãã
jdeps --list-deps --ignore-missing-deps myapp-1.0.jar
ä¸è¨ã®ããã« jdeps ã³ãã³ãã«ä½æããjarãã¡ã¤ã«(ããã§ã¯ myapp-1.0.jar)ãæå®ããã¨ãä¾åã¢ã¸ã¥ã¼ã«ãåæããã¦åºåããã¾ãã
java.base java.desktop
ããã»ã©ä½ã£ããµã³ãã«ã¢ããªã¯ Javaæ¨æºã® Swingã©ã¤ãã©ãªã§ã¦ã£ã³ãã¦ãä½ã£ã¦ããã®ã§ãjava.desktop ãªã©ãåºã¦ãã¦ãã¾ãã ãµã³ãã«ã¢ããªãåããã«ã¯JREã®ã¢ã¸ã¥ã¼ã«ã®ãã¡ã java.base 㨠java.desktop ã ããå¿ è¦ãªã®ã§ããã®2ã¤ã ããæ½åºããJREã jlink ã§ä½ãã¾ãã
jlink ã使ã
jlink ã³ãã³ã㯠--add-modules ãªãã·ã§ã³ã®å¼æ°ã§å¿ è¦ã¢ã¸ã¥ã¼ã«ãã«ã³ãåºåãã§æå®ãããã¨ã§ãå¿ è¦æä½éã®JREãæ½åºãã¦ããã¾ãã ã¾ãã--module-path ãªãã·ã§ã³ã§ãæ½åºå ã®ã¢ã¸ã¥ã¼ã«ã®æ ¼ç´å ´æãæå®ããå¿ è¦ãããã¾ãã Windowsåãã«ãã«ãããå ´åãWindowsç¨ã®JREã¢ã¸ã¥ã¼ã«ãå¿ è¦ã«ãªãã®ã§æ³¨æãå¿ è¦ã§ãã macã§ä½æ¥ãã¦ããå ´åãå¥éWindowsç¨ã®JDKãzipã§ãã¦ã³ãã¼ããã¦å±éããå¾ãjmodsãã£ã¬ã¯ããªãæå®ããå¿ è¦ãããã¾ãã
jlink --compress=2 --module-path ~/Downloads/jdk/jmods --add-modules java.base,java.desktop --output ./product/jre-min
launch4j ã使ã
ãã¦ãããã§jarã¨æå°JREãä½ããã®ã§ããã¨ã¯exeã«ããä½æ¥ã«ãªãã¾ãã ããã«ã¯ launch4j ã¨ããOSSãç¨ãã¾ãã launch4j ã¯GUIãããã¾ãããGradleã®ã¿ã¹ã¯ãç¨æããã¦ããã®ã§ããã¡ããå©ç¨ãã¾ãã build.gradle 㧠launch4j ã® plugin ã追å ããè¨å®ãè¨è¿°ããã°ããã¨ã¯ launch4j ã¿ã¹ã¯ãå®è¡ããã ã㧠exe ãã¡ã¤ã«ãåºåããã¾ãã launch4j ã®è¨å®ã§éè¦ãªã®ã¯ã bundledJrePath ãªãã·ã§ã³ã§ãã ããã§exeãåç §ããJREã®å ´æãæå®ã§ããã®ã§ãç¸å¯¾ãã¹ã§å ã»ã©ä½æããæå°ã®JREã®ãã£ã¬ã¯ããªãæå®ãã¾ãã
plugins { id 'java' id 'edu.sc.seis.launch4j' version '2.5.3' } launch4j { mainClassName = 'jp.example.Application' outputDir = './product' bundledJrePath = './jre-min' bundledJre64Bit = true }
ä¸é£ã®æµããGradle ã¿ã¹ã¯ã«ã¾ã¨ãã
以ä¸ã§ exe ãä½ããã¨ãã§ãã¾ãããã jdeps ããå§ã¾ãä¸é£ã®æµããæä½æ¥ã§å®æ½ããã®ã¯éª¨ãæããã®ã§ãGraldeã¿ã¹ã¯ãä½ã£ã¦ã³ãã³ãä¸ã¤ã§ exe ãä½æã§ããããã«ãã¾ãã ã¾ããexe ã¨å ±ã«æå°JREãã¾ã¨ãã¦ã¢ããªãé å¸ããå¿ è¦ãããã®ã§ãå ¨ä½ãzipã«ã¢ã¼ã«ã¤ãããã®ãè¯ãã§ãã ãããGradle ã¿ã¹ã¯å ã§æ´åãã¦ããã¾ãã
jdeps 㨠jlink ã¯ä»¥ä¸ã®ããã«ã·ã§ã«ã¹ã¯ãªããã«ã¾ã¨ãã¦ä½¿ããã¨ã«ãã¾ããã
#!/bin/sh JAR_PATH=build/libs/myapp-1.0.jar #Jarãã¡ã¤ã«ã®ãã£ã¬ã¯ã㪠OUT_PATH=build/product #JREåºåå ãã£ã¬ã¯ã㪠WIN_JAVA_DIR=~/Downloads/jdk #Windowsç¨ã®JDKã®ãã£ã¬ã¯ã㪠jdeps --list-deps --ignore-missing-deps $JAR_PATH > tmp.txt DEP_LIST="" while read LINE; do if [ -z "$DEP_LIST" ]; then DEP_LIST="$(echo $LINE)" else DEP_LIST="$DEP_LIST,$(echo $LINE)" fi done < tmp.txt rm tmp.txt JRE_DEST=$OUT_PATH/jre-min if [ -e "$JRE_DEST" ]; then rm -rf $JRE_DEST fi jlink --compress=2 --module-path $WIN_JAVA_DIR/jmods --add-modules $DEP_LIST --output $JRE_DEST
ãã¨ã¯Gradleã§ãjarä½æ->JREä½æ/exeä½æ->zipä½æ ã®æµããå®ç¾©ããã°OKã§ãã ã¾ã createJREã¿ã¹ã¯ãå®ç¾©ããå ã»ã©ã®ã·ã§ã«ã¹ã¯ãªãããæå®ãã¾ãã jdeps 㯠jarãåç §ããã®ã§ãdependsOn ã« jar ã¿ã¹ã¯ãæå®ã㦠jarä½æå¾ã«å®è¡ãããããã«ãã¾ãã
次㫠createZIPã¿ã¹ã¯ãå®ç¾©ããlaunch4j ã¿ã¹ã¯ã¨ createJRE ã¿ã¹ã¯ã dependsOn ã«æå®ãã¾ãã ããã§ãZIPã¿ã¹ã¯ãå®è¡ããã°ãä¸é£ã®æµããå®è¡ãããããã«ãªãã¾ãã
task createJRE(type: Exec, dependsOn: ["jar"]) { workingDir '.' executable './create_jre.sh' } task createZIP(type: Zip, dependsOn: ["launch4j", "createJRE"]){ archiveFileName = "product.zip" from 'build/product' }
çæãããzip ãã¡ã¤ã«ãwindows ç°å¢ã§éãã¨ãç¡äºã«ã¢ããªãå®è¡ããã¾ããã
è£è¶³: jlink ã® Gradle ã¿ã¹ã¯
ã¡ãªã¿ã«ãä»åã¯jdeps, jlink ã®æµããã·ã§ã«ã¹ã¯ãªããã§å®è¡ãã¾ããããjlink ã® gralde ãã©ã°ã¤ã³ãããã¾ããã
plugins{ id 'org.beryx.jlink' version '2.21.0' } jlink { mainClass = 'jp.example.Application' options = ['--compress', '2'] targetPlatform("win", "~/Downloads/jdk") }
ãã®ãã©ã°ã¤ã³ã使ãå ´åããããããä¾åã¢ã¸ã¥ã¼ã«ã¯ module-info.java ã«è¨è¿°ãã¦ããå¿ è¦ãããã¾ãã
module myapp.main{ requires java.base; requires java.desktop; }
Java 17 ã® jpackage
æå¾ã«ãJava 17ã§ã¯ãmsiãexeã®ã¤ã³ã¹ãã¼ã©ãä½æã§ãã jpackage ãå°å ¥ããã¦ããã®ã§ããã¡ããå°ãã ããç´¹ä»ãã¾ãã
jpackage ã使ãã¨JREãå梱ããã¢ããªã±ã¼ã·ã§ã³ã®ã¤ã³ã¹ãã¼ã©ã msi ã exe dmg ãªã©ä»»æã®å½¢å¼ã§ä½æã§ããããã§ãã ã¢ããªã® jarãã¡ã¤ã«ãä½æãã¦ããã jpackage ã³ãã³ãã以ä¸ã®ããã«å®è¡ããã° exeãã¡ã¤ã«ãåºåããã¾ãã
jpackage --name MyApplication --input build/libs --main-jar myapp-1.0.jar --module-path ~/Downloads/jdk/jmods --add-modules java.base,java.desktop --win-shortcut --dest build/product
--module-path ã --add-modules ãªã©ã®ãªãã·ã§ã³ã¯ jlink ã¨åãã§ãã --runtime-image ãªãã·ã§ã³ã§æå°JREãç´æ¥æå®ãããã¨ãã§ããããã§ãã
ä¸è¨ã§ã¯ããã«ã --win-shortcut ãªãã·ã§ã³ã§ãwindowsã§ã®å®è¡æã«ã¤ã³ã¹ãã¼ã«å¾ãã¹ã¯ãããã·ã§ã¼ãã«ãããã§ãããããªè¨å®ãã¤ãã¦ãã¾ãã
çæããã exe ã windowsç°å¢ã§å®è¡ããã¨ãããã¢ããªãã¤ã³ã¹ãã¼ã«ããããã¹ã¯ãããã«ã·ã§ã¼ãã«ãããçæããã¾ããã ã·ã§ã¼ãã«ãããå©ããã¨ã§ã¢ããªãèµ·åãã¾ããã
å ã»ã©è£è¶³ã§ç´¹ä»ãã jlink ã® Gradle ãã©ã°ã¤ã³ã« jpackage ã¿ã¹ã¯ãããããããã¡ãã§ã exe ãçæã§ãã¾ãã
ãã¨ãã
ä»åJava ã¢ããªã±ã¼ã·ã§ã³ãWindowsåãã«å®è¡å¯è½ãªç¶æ ã§é å¸ããã«ã¯ã©ãããã°ãããæ´çãã¦ã ã²ã¨ãã« Java ã¨ãã£ã¦ãããã¼ã¸ã§ã³ã«ãã£ã¦ã©ãã¢ããªãé å¸ããã°ãããé¸æè¢ãå¤ãã£ã¦ãããã¨ããããã¾ããã
ç¹ã«Java 8 ãã Java 11 ã«ç§»ãä¸ã§ãJREãæå°åãã¦å梱ã§ããããã«ãªã£ãã®ã¯å¤§ããªå¤åã ã¨æãã¾ãã ãããããæ°ãã¼ã¸ã§ã³ããªãªã¼ã¹ããã¦ããä¸ã§ãJavaã§ã®ãã¹ã¯ãããã¢ããªéçºã®æé©è§£ãã©ã®ããã«å¤åãã¦ããã注è¦ãã¦ããããã¨æãã¾ãã
P.S.
æ¡ç¨
èã®ç©´ã§ã¯ä¸ç·ã«åã仲éãåéä¸ã§ãï¼
ãã®è¨äºãèªãã§ãèå³ãæã£ã¦ããã ããæ¹ã¯ãã²å¼ç¤¾ã®æ¡ç¨æ
å ±ãã覧ä¸ããã
yumenosora.co.jp
LINEã¹ã¿ã³ã
ã¨ã³ã¸ãã¢å°ç¨ã®ã¡ã¤ãã¡ããã¹ã¿ã³ããå®æãã¾ããï¼
ããã®å ´é¢ãã§æãã使ããããªããããªã¹ã¿ã³ããããæ¥å¸¸ã§å½¹ç«ã¤ã¹ã¿ã³ããåè¨40åç¨æãã¾ããã
ã¨ã³ã¸ãã¢ã®çãããã¨ã³ã¸ãã¢ã§ãªãæ¹ããã²ã¹ã¿ã³ãã確èªãã¦ã¿ã¦ãã ããã
store.line.me