Launch a script from a .jar [duplicate] - java

This question already has answers here:
Executing a shell script inside a jar file. How to extract?
(4 answers)
Closed 2 years ago.
I've created a .jar in which I have a script named init.sh
I got this error.
tim#TS:~/Desktop$ java -jar test.jar
java.io.IOException: Cannot run program "/init.sh": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:621)
at java.lang.Runtime.exec(Runtime.java:486)
at Main.run(Main.java:42)
My code which works when I run on Eclipse is below.
String[] cmd = {"src/init.sh"};
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I've tried to change it without success like that.
String[] cmd = {"init.sh"};
I looked at my .jar file and the init.sh script is at the same level as the Main.class.
My question is : How must I change my code if I want to get a functional .jar ?

Based on the answer here Executing a shell script inside a jar file. How to extract?, looks like the shell script needs to be extracted to a location outside the jar file before you can execute it.

Related

Giving command line arguments to java program over git-bash

I'm currently working on a program to zip and unzip files/directories and it behaves kind of weird when I call it over the git bash.
The program takes three arguments (zip/unzip, inputPath, outputPath).
For Example:
java -jar zip_unzip.jar --zip H:\\zip_test H:\\test5\zip_test_4.zip
Everything works fine when I call it over Eclipse or CMD. It creates the directory structure if it doesn't already exist and zips the input Folder into the newly created output folder. But when I call it over the git bash like in the example above it somehow "ignores" the backslash and instead of creating a folder called test5\, creates a zip-archive called test5zip_test_4.zip
Here's a snippet of the code that takes care of creating the directory structure, where zippedFolder is the outputPath-Parameter:
File directoryToZip = new File(inputFolder);
String targetZippedFolder = zippedFolder;
targetZippedFolder = targetZippedFolder.replace("\\", "/");
//create directory to store archive in, if it doesn't already exist
File destDir = new File(targetZippedFolder.substring(0, targetZippedFolder.lastIndexOf("/")));
if (!destDir.exists()) {
destDir.mkdirs();
}
In line 3 of the codesnippet I'm replacing every backslash with a forwardslash because I thought this would make it platform-independent.
Can someone explain the behavior of the git bash to me and maybe suggest a more platform-independent path handling method?

Java cant run jar with processbuilder [duplicate]

This question already has answers here:
CreateProcess error=2, The system cannot find the file specified
(6 answers)
Closed 5 years ago.
Heres my code please help! I'm making a launcher for my game. I want to be able to launch the game from the launcher but it dosent work.
ProcessBuilder pb = new ProcessBuilder("test.jar","C:/Users/Marcus/Documents/");
try {
pb.directory(new File("C:\\"));
Process p = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
error:
java.io.IOException: Cannot run program "test.jar" (in directory "C:\"): CreateProcess error=2, Cant find the file at java.lang.ProcessBuilder.start(Unknown Source)
Your test.jar is relative that's why i cannot find the it.
You have to specify the path to test.jar
Then you should check if your jar is executable. If not you will need to add java to the list of arguments when creating the ProcessBuilder.

How to close an open file in java [duplicate]

This question already has answers here:
How to check if a file is open by another process (Java/Linux)?
(5 answers)
Closed 6 years ago.
I am trying to write an excel file in java, and saving it in a shared folder. but if the file currently open at any system by anyone, it is throwing an error. So is there any way that we can close an excel file via a java command?
How to delete/close open excel file. so you have to auto close the file before delete...
final File file=new File("E:\\book1.xlsx");
Runtime.getRuntime().exec(
"cmd /c taskkill /f /im excel.exe");
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.currentThread().sleep(2000);// you need to wait 1-2 sec to close file before delete
file.delete();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
If the file is actually open in someone's window (like you would open a ms word doc), then it's not going to be possible to close the file on their machine from a java program. If you just need to one-time update that file, I'd recommend creating another file with a different name, and then manually replacing the contents of the desired destination with the contents of the file that you programatically create.
Depending on the way the file is opened, you may be able to execute the command net files <id> /close via Runtime.exec(). You can also combine the psexec command to close files on remote computers.
This answer seems to describe a similar situation, you just need to make java run the commands:
https://superuser.com/questions/643916/remotely-close-open-shared-files

Executable jar closes immediately after launching (Slick2D)

So I made an executable file with Eclipse but when I double-click it nothing happens. It IS NOT because I've got wrong association for running .jar files (I'm running okey my another jar program just by double-clicking it).
I also tried with command prompt and the console doesn't print anything at all and get back to be "normal".
I think it's something with main class so the thread doesn't last but closes after running (however it looks like the code isn't run because console would print lot of Slick stuff).
So here is the main method:
public static void main(String[] args) {
Shiftjumper shiftjumper = new Shiftjumper("Shiftjumper - behavior prototype");
try {
AppGameContainer gc = new AppGameContainer(shiftjumper);
gc.setDisplayMode(TILE_SIZE*RIGHT_LEFT_TILES, TILE_SIZE*UP_DOWN_TILES, false);
//gc.setTargetFrameRate(60);
gc.start();
} catch (SlickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And here the manifest:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ lwjgl.jar slick.jar
Class-Path: .
Rsrc-Main-Class: engine.Shiftjumper
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
You need to download a tool known as JarSplice. JarSplice is a very simple GUI based application which packages all libraries and native code into the form of either a .JAR, a .EXE, a.SH or a .APP. Here is a link to the site: http://ninjacave.com/jarsplice
Hope this helps :)
Try running the jar in your terminal or command prompt, if there are any exceptions it will print to the console and you can debug

JSmooth generated exe does not show Splash Screen

I wrap my Java Swing application as an exe using Jsmooth but I can see no way to take advantage of Java 6 splash screen option. I have the following manifest file:
Manifest-Version: 1.0
SplashScreen-Image: resources/LOADLOGO.png
Main-Class: se.bookingapp.UI.MainFrame
The splash screen appears if I simply click on the jar file of the application. However, the JSmooth generated exe form of the jar file does not show the splash screen somehow. Does anyone know why?
Yesterday I've finished to develop my java application and I had the same issue. If I double click the .jar file or I execute in a command line splash screen works perfectly, but when I execute the wrapped file it doesn't. Seems just JSmooth doesn't support this feature.
However I made a little trick to have a wrapped .exe and splash screen working at the same time. I made a little application called ApplicationLoader.jar that consists in a single main class that execute java -jar "Application.jar" in a command line. Here is the complete code:
public class ApplicationLoader {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
/* First I check if the first parameter is not null and it's not an empty string */
if(args[0] != null && !args[0].trim().isEmpty()){
/* Then I use java.util.regex package to validate the parameter is a .jar file */
Pattern pattern = Pattern.compile(".*jar");
Matcher matcher = pattern.matcher(args[0]);
if(matcher.matches()){
/* Finally I define the command line like: java -jar "Application.jar" */
String command = "java -jar \"" + args[0] + "\"";
try {
Runtime r = Runtime.getRuntime();
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", command);
Process p = pb.start();
p.waitFor();
} catch (IOException | InterruptedException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error executing: "+command, JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "The argument is not a .jar file!!");
}
} else {
JOptionPane.showMessageDialog(null, "There's not a valid argument!");
}
}
}
I have this folder structure for my application:
MyApp
+-- bin
| +-- MyApp.jar
| +-- ApplicationLoader.jar
+-- MyApp.exe
So in JSmoot I changed the classpath to ApplicationLoader.jar and add the relative location to my application in Application Arguments section like this:
And that's it. I know this is not the best option but is a workaround.
However there's a little problem:
Since ApplicationLoader.jar calls a cmd.exe then the wrapped .exe and your application will execute in two different processes.
This implies that if you have to kill .exe process for some reason (unexpected crash or something), your java application still working as a java.exe process. So in that case you must kill MyApp.exe and java.exe processes. Actually if you just kill java.exe process then MyApp.exe process will finish execution by itself.
If you keep this in mind and you can live with that I think this option is quite simple and useful.
I hope this be helpful to anybody looking for a workaround to this issue.
Does it work when you execute the jar file? Open it with WinRar for example, and check if the manifest is into META-INF folder, and LOADLOGO.png is in the right folder too.
After doing that, it should work. It works for me. Nothing wrong in your manifest.

Categories

Resources