How to run a jar file using specefic runtime parameters - java

I have a .jar file from a project I build, but whenever I run it, I use specific flags in Terminal (specifically -xDock). Is there a way to configure a .jar so that when I double click on the file it will run using whatever flags I use in terminal?

If you want to make it the Java way you can change the Java code to have default parameters - then you can just run it without. Or you can make it take its configuration from a config file and once you change that config file it will not require additional params.
If you have no control on the java code and you have a specific jar you want to double click then you can do it the windows way ;) Create a shortcut and in the Target: field add the jar with parameters
Or you can create a batch / sh file that runs it with specific params

If the parameters aren't dynamically changeable write them hard-coded or use this solution as default parameters when other parameters aren't passed.

Another interesting way of doing this, is by using JarSplice.
It's a tiny program that lets you add arguments into a sort of "wrapper" around the Jar. Then when you open it up, the arguments will be passed with it.

Related

how to change .class file to a text file

I'm trying to make a java program which executes java files and gives output in the text field. I've used Runtime class to compile the .java file .So how do I get the output from that newly made class file.
Runtime.getRuntime().exec("javac Y://CodeSave.java");
Runtime.getRuntime().exec("java Y://CodeSave.class>output.txt");
In the general case: exec returns a Process instance which has accessors (getOutputStream, etc.) for the I/O streams. You read from / write to those streams.
But: In your code you've used >output.txt. That's a shell feature. If you want to do it that way, you need to spawn a shell, not the java tool directly, and have the shell execute that command line. (A search for spawning/execing a shell should find you lots of examples.)
Using Runtime.exec is definitely not the right way to do it, for various reasons. Examples are that both java and javac rely on environment variables which you can't pass that way.
First of all, I'd ask myself if I really needed to do this. Compiling and executing dynamically created code is a huge security risk.
But if you're sure you need to do it, here's what I'd do.
Move your sources to a dedicated temporary folder
Use the ToolProvider api to compile your sources
Use a dynamic throwaway ClassLoader (ByteBuddy may help you there) with a SecurityManager to load and execute your code from within your application

creating my own file type and making it run another file?

I'm currently writing my own programming language for my senior project and using java to compile it. (in a sense where it reads through a text file and pulls out keywords and phrases and acts on them how i define). i would like to be able to have my text file be of type ".SeniorProject" for example, and then double click it and then it direct to the actual java program that reads it and acts upon on, also i would like if it would take and give input/output through command prompt instead of eclipse which i'm currently using. could someone direct me to a guide to set this up or explain it if possible. i will be using a windows device for this, not linux so i know that makes it more complicated for me wanting to give and receive input from the command prompt.
First of all, to run the program outside of eclipe, you can compile it into a .jar file (Java executable). In eclipse, you can do this by going to file -> export -> JAR file. Afterwards, you can simply type java <yourfilename>.jar in the command prompt and use the program like this.
Associating the file extension with your jar is a little bit trickier, in theory you can associate any file extension to any executable, but only windows executables.
You could maybe create a .bat file that simply starts your jar on the jvm or use an exe wrapper like Launch4j to create an exe form your jar file.
To expand on #Gumbo's answer, you can make a .bat file that runs
java javaprog.jar %*
or
java <any other args you want go here> JavaProg %*
If you then run thisbatfile.bat c:\my.seniorProject then your Java program will run and its Main method will have access to "c:\my.seniorProject" through its arguments variable.
To make it easier to run these, you can associate that .bat file with ".seniorProject" files.
You can associate your new batch file with .seniorProject files one of two ways:
(easy, manual) Double-click on a file that has the ".seniorProject" extension. That will bring up a dialog that will let you choose your batch file as the preferred program used to open ".seniorProject" files.
(harder, but you can script it and you can specify separate 'edit' and 'run' options that will display in the right-click menu) Use assoc to make a new file type alias and then use ftype to associate the 'open' action of that alias with your batch file.
Once the association is done, you can just double-click a .seniorProject file to run it.

get the root directory of a maven application

I need to provide a settings file for my program, to which the user should have access to write some of the settings i need.
I created a file under a new directory (called settings) on the root of my application, but i have problem finding it at run time.
I use
File SettingsFile=new File(ClassLoader.getSystemClassLoader().getResource(".").getPath()+"settings/CreateSettings.txt");
When i execute this under eclipse i get
/application/home/dir/target/classes/settings/ZipCreateSettings.txt which is wrong.
If i execute it on terminal using java -jar, i get the correct path,
/application/home/dir/settings/ZipCreateSettings.txt
This would cause me problems cos i need to run the application directly from eclipse and not use the terminal, even though it is going to be executed using the jar when it is up and running.
I cant keep it like that anyway, cos this code might end up in someone else's hands, and they would have no idea what to do with it.
I have also used some other techniques like
new java.io.File("").getAbsolutePath(); but this always gives me the current working directory, so if i execute the jar from /home, i would get /home.
I think the problem might be maven (which i am not familiar with at all) since my code worked with a plain java application some time ago.
Since your file will be located outside your classpath, it is basically outside your application. Your application is not aware of files existing outside it's classpath. So you will need some kind of way to provide the Full/Absolute path to your file. You can't use the classloader in your case.
I suggest you use a system param instead of a hardcoded value. See here

Java - Arguments in Jar File executable

Basically I've created a jar file, but I want it to have precreated arguments.
E.g. like the below add ons.
-Xmx256m
The problem is I'm not too sure how to make it so this is embedded into the Jar file let alone if it's even possible. I'm asking for it to not require a batch or command prompt addition, but to simple remain clickable as a jar file. Then add the extra arguments I desired upon clicking. Would this be a manifest modification possibly?
I've been searching Google and other links on this website with very little luck to the answer I desire.
There is no way to make a clickable jar with JVM settings.
After you double click it, you're calling "java -jar" and then your JVM will be running before reading jar contents.
I don't think this is possible for JVM args without some hackery. The reason being that the JVM instance is already initialized by the time you get to the main() method.
A potential hack would be to change your main method to make a System call to spawn another java process with the arguments you desire.

Eclipse run configuration to run remotely using a script

I would like to use a run configuration (or something similar) to run a class from Eclipse using a shell script. The shell script will do a bunch of fancy stuff to make the job run on a machine with more memory. That part I can do.
What I don't know is how to make Eclipse pass the class name and class path to a shell script - and ideally show the output of the shell script in the console window. This seems like it should be simple. I'm using 3.5.2.
Thanks!
I'm not sure of a clever way to pass your classpath to your script, but the External Tools Configurations can do this. And the output will be printed in your console. There are Variables that can be set up to manage your classpath a bit better than I have done below. Though I've used the working directory as my classes folder, so I only need to use . as my classpath.
Variables can be used throughout to set up common paths etc.

Categories