Passing arguments to JAR which is required by Java Interpreter - java

When i execute my java application i need to pass arguments which is needed by Java interpreter for Flash image.
for eg. java -sum_arg Demo
Now i want to create JAR file for my application and how can i overcome need of arguments.

You can pass arguments to Java applications packed into .jar files just as you can with single .class files:
java -jar MyApp.jar anArgument
If you care about the UI case where a user simply double-clicks the .jar file to run your application, then you might want to consider using a real UI (using Swing, for example).
A simple way to get input this way would be with a JOptionPane:
String myInput = JOptionPane.showInputDialog("Enter something!");

If you have not any class path dependencies
java -jar test.jar "arg1" "arg2"
If you have any class-path dependencies
java -cp classpath-dependencies -jar test.jar "arg1" "arg2"
In swing we can get parameter from command line argument.
public static void main(String[] args)
{
for(String arg : args)
System.out.println(arg);
}

Related

Using java.exe with .class file from Eclipse

I have a programming working in Eclipse. I need to be able to send the project to my instructor, who will run it from the command line with java.exe.
The program takes arguments, and I have tested those within Eclipse and they work fine, however, when I attempt to use java.exe to execute the main.class file that is in the bin folder of my project I get the following error:
Error: Could not find or load main class C:\Workspace\Sort\bin\Main.class
I have JRE 1.8.0_60 installed on my system and not the JDK. However, whatever Eclipse uses to compile, I should be able to use as well. I know I can export it as a JAR, but that is not part of the assignment and I don't think JAR files take arguments.
Any ideas?
Keep in mind that the default classpath for java.exe only includes the current working directory. So if you're not executing java from the bin/ folder of your project, it won't know where to find your class. You need to either run from C:\Workspace\Sort\bin or use the -cp option to include that folder on the classpath. For example:
java -cp C:\Workspace\Sort\bin Main arg1
Also note that you should not include the .class file extension when specifying the name of the class.
You can send parameters when executing a jar :
using arguments
cmd: java -jar Myapp.jar arg1 arg2
code:
public static void main(String[] args) {
//args[0] = arg1
//args[1] = arg2
}
using parameters:
cmd: java -Darg1_name=arg1 -Darg2_name=arg2 -jar Myapp.jar
code:
public static void main(String[] args) {
//System.getProperty("arg1_name") = arg1
//System.getProperty("arg2_name") = arg2
}

How to build an executable program using eclipse, there are parameters for the program

How can I build an executable program written in java using eclipse? and there are arguments need to be passed in ubuntu terminal.
the main function is like this:
public class Sample {
public static void main(String[] args) {...}
and I would like to call it in terminal like this:
$./program args[1] args[2]
The answers to this question give a number of options for giving a java program an executable wrapper: How can I convert my Java program to an .exe file?
That said, my preference for use on linux tends to be just bundling my compiled classes as a jar (eclipse provides an export to jar tool) and writing a shell script like:
#!/bin/bash
export CLASSPATH=`dirname $0`/my-archive.jar
java -cp $CLASSPATH my.package.Sample $#
That sets the classpath relative to the location of the script (so you can call the script from anywhere) and passes any arguments given to the script along to the java program.

Pass command-line argument javaagent with maven exec plugin

I have a caching app in Java and I need to put objects of different size in cache. The problem is that I didn't really know how to count the size of a custom object and I've found the solution - to use the library: http://mvnrepository.com/artifact/com.googlecode.sizeofag/sizeofag/1.0.0.
To run the program using the library I need to specify command-line argument -javaagent. So, how can I do it if I'm using maven???
The program is simple:
protected static Boolean b;
public static void main( String[] args )
{
System.out.println(SizeOfAgent.sizeOf(b));
}
This is the output:
0
Can not access instrumentation environment.
Please check if jar file containing SizeOfAgent class is
specified in the java's "-javaagent" command line argument.
P.S. I know, that such kind of question already exists, but it has no proper answer.
On a Linux/Unix machine the "mvn" command will use a shell variable "MAVEN_OPTS" to pass in options. This is useful if you want to give Maven more memory. In your .profile or .bash_profile put a line like this in:
export MAVEN_OPTS=-javaagent
On windows:
in shell (cmd.exe) type "set MAVEN_OPTS=..."
or
add MAVEN_OPTS to your environment
On NetBeans:
In ~/.netbeans/6.5/, create etc/netbeans.conf. Add your environment variables there, e.g.:
export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=128m"

How to handle files that opened a program

When my Java file-processing program is opened by an Open With... command, or is set as a file's default program, how do I handle the file that opened it.
Is it passed as a command line argument?
In what format?
And how about programs, wrapped in an .exe wrapper, or compiled with an AOT compiler?
Launch the app. with Java Web Start and declare an interest in the file-type within the launch file (JNLP).
The path to the File will be passed as a String as the 2nd argument to the main. The 1st argument will be either -edit/open (I forget) or -print.
And how about programs, wrapped in an .exe wrapper, or compiled with an AOT compiler?
How about asking that on a separate question? If deploying with JWS, we would use Jar(s).
Create an executable of your Java File processing program. Please read this-creating executable file if you want to know, how to create executable?
In command line, you may say: executable FileName.ext
FileName.ext will be available in your main program's args[0] attribute.
i.e.
public static void main(String[] args){
String fileName = args[0];
}
You should receive the file's path as an argument in main().
See Using command-line argument for passing files to a program (maybe duplicated?)

System.out.println in jar

I have this java program that I want to package within a jar.
It compiles fine and it compiles without errors into a jar. But when I double click it, it wont start. I know most jar applications uses JFrame and that works fine. But is it possible to make it command prompt/shell based? (Like Displaying System.out.println)
Example is it possible to execute this code by double clicking a jar:
public class Hello
{
public static void main( String[] args )
{
System.out.println( "Hello, World!" );
}
}
There is no problem doing like that. But where do you expect to see the output?
If you execute from the console as java -jar my jar.jar you will see your "Hello, World".
If you want to double-click you'll need to create a JFrame.
You can change the file association for jar files to have them open a console.
(This is for Windows)
Open Command Processor
Type ftype jarfile
You will get something like:
jarfile="C:\Path\To\Java\bin\javaw.exe" -jar "%1 %*"
Enter ftype jarfile "C:\Path\To\Java\bin\java.exe" -jar "%1" %* (You may need administrator privileges to do this).
I don't see anyone mentioning the obvious solution: make a .cmd (or .bat) file containing the command everyone is talking about -- java -jar YourJar.jar. You can double-click on the .cmd file and a console window will open. It will also close imediately as your program exits, so the program should wait for a keypress before exiting.
You can make a jar an executable by including a manifest file which contains the name of the class that has your main method (in your example that would be Hello). Here is a link that details what you need to do.
Sure. First try to run your program as following:
java -cp yourjar.jar Main
where Main is a fully qualified class name of your main class.
When this is working fine and if you creted exacutable jar try to run your program as following:
java -jar yourjar.jar
When this is working double click should work too unless you mapped extension jar to program other than javaw that is done by default when you are installing JRE.
If you want to display the "Hello, world!" String you have to execute your code from the command line:
jar -jar your_jar_name.jar
The output gets forwarded to the console but without eclipse, there isn't any! The only way to work around this is to launch the program in command prompt and then you will get the output from the program.

Categories

Resources