-3

I am trying to figure out to make a startup script for my java application, I tried making an installer for this in advanced installer. Over there I added the jvm arguments but it did not work, well that is a separate issue...

What I am asking is: Is there a script that I can place along with my JAR file in C Drive so that from the Desktop's Shortcut I run that script and that in turn opens the JAR with increased heap space?

I couldn't neither find how to make startup script on Google, Sorry I am not too familiar/aware of Bat file scripting

This question is somewhat sort of an extension to my last question

Community
  • 1
  • 1
Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
  • 2
    Downvoter please comment. – Daksh Shah May 14 '14 at 09:35
  • Does your JAR have dependencies? – Edmondo May 14 '14 at 09:36
  • @Edmondo1984 Yes I use many of them like TTS – Daksh Shah May 14 '14 at 09:37
  • In that case you need to build a Fat JAR which contains all the dependencies or the code won't run. If you have a fat JAR the answers below are correct, if instead you don't you need to build it. – Edmondo May 14 '14 at 11:45
  • @Edmondo1984 I don't know whether those are called dependencies or not, but I have some JARs that I have added like freeTTS.jar and some apache libs.... And I don't even know what you mean by FAT Jar or how to create it... I am feeling really dumb here :( – Daksh Shah May 14 '14 at 12:04

3 Answers3

3

You could create a Batch script file ".bat" that runs a command like -

java JVM_Arguments -jar yourJarFile.jar
pause

pause is optional if you want to stop the command prompt from closing at the end of jar execution - to check for errors or just the output of the execution.

A. Agarwal
  • 415
  • 4
  • 12
  • 1
    Don't forget to add `pause` on a new line in the bat file or else the command prompt will exit so fast you won't get to see your errors. (Unless of course you run it from a command prompt). – SSpoke May 14 '14 at 09:46
  • This seems to be working, I will accept the answer after I am assured that it is working. For now I have +1ed :D Thank You – Daksh Shah May 14 '14 at 09:46
  • @SSpoke Why would I need that? I will add this after I am done with development. This is for end-user... Will I still need that? – Daksh Shah May 14 '14 at 09:47
  • @DakshShah You need the `pause` potentially only for development. Alternatively you can open a command prompt and run the batch file from there. – A. Agarwal May 14 '14 at 09:49
  • you need `pause` for development to see your errors, when you release it to user you can remove it.. but I would still keep it.. (the user may still get errors like they may not have java installed.. they must see it to report problem or they may think it's a virus LOL cuz it closes very fast and does nothing. – SSpoke May 14 '14 at 09:50
  • @SSpoke What is the syntax for pause in batch scripting, just `pause` after that line? – Daksh Shah May 14 '14 at 09:51
  • 1
    @SSpoke Ok thanks now please add this to the answer and delete the comments so that it is nicely formatted for future viewers to go through – Daksh Shah May 14 '14 at 09:53
  • @DakshShah (This comment is useless though haha). I edited the answer although it is optional so it might get rejected, but yeah I'll leave the comments so people can read the whole story I always read it to understand the big picture myself. Yup 2 people rejected it.. I got lucky it went though. – SSpoke May 14 '14 at 09:57
1

java -jar PATH_TO_YOUR_JAR in a batch file should do this. but the condition is your jar should be executable.

Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77
1

Typically a JAR has external dependencies which are not packaged as a part of it, but needs to be provided to the Java Virtual Machine when running the JAR (they need to be in the classpath)

If you want to run your class from the command line you have several solutions:

  • Create a batch script as suggested above or in Run class in Jar file . You will have to carefully provide with the -cp argument all the jars required
  • Create a "fat" jar that contains inside all the classes from external jars. This might generate a JAR of several megabytes if you need many libraries. In such a case writing the batch script will not require you to add all the jars, because they will be contained in the fat jar
  • Launch the application using a build system, capable of writing for you the call to the java executable with the right parameters

The general suggestion is that you get familiar with class loading in Java and just after with build systems, which are going to become your best friend whenever developing a non trivial application. I would probably suggest you start with Maven, even if today better tools such as Gradle or SBT are available

Community
  • 1
  • 1
Edmondo
  • 19,559
  • 13
  • 62
  • 115