How to shorten Java -jar command in windows terminal - command-line

Is there a way to set a jar to behave like a regular batch file in windows, meaning that i only have to type MyJar param1 param2 instead of java -jar MyJar.jar param1 param2 ? I am looking for a global solution that i only have to setup once, not a Jar Specific solution such as creating a batch file for every jar i wish to run.
Thanks in advance

Related

Eclipse run configuration annoyance

If I run a jar as follows
java -jar Name.jar arg1 arg2 arg3
it is understandable that when exporting (creating the .jar) I would have had to specify an Eclipse run configuration in order to identify the main method because there may be as many main methods as there are classes.
If I run a main program from within Eclipse, it silently creates a new run configuration which adds more clutter to the run configuration choices available when I want to export. If I do this for many classes, the chances are I have many essentially identical run configurations, with the only difference being the main method specified.
If I run a jar as follows
java -cp Name.jar package.MyObject arg1 arg2
java -cp Name.jar package.MyOtherObject arg1
then the Eclipse run configuration which identifies the main method is probably ignored. The entry point is identified on the command line. In this case, the fact that Eclipse requires a run configuration during the export process, seems to create a chore, but there seems to be no way to avoid this.
I prefer to run my .jar via the -cp option because it gives me access to many entry points. This way I don't have to re-export when I want to access a different entry point. The decision as to which entry point to use is postponed until the time I want to run. I don't have to decide when exporting. I preserve flexibility.
I know an alternative way is to just have an arg1 that specifies the task, so I can rely on just one run configuration that identifies the main method that has a big switch statement and then always invoke Java with the -jar option.
If I use -jar there is the clutter of many run configurations. If I use -cp there seems to be an nuisance step in the export process that involves the clutter of many run configurations. Is there a way to use Eclipse that avoids both of these problems?
Open the project and select File and Export and Java and JAR file. Do not select Runnable JAR file.
By default, every file in the project folder is check-boxed as a resource meant to be exported and you may want to un-check some or all of them. Unfortunately Eclipse does not remember this so you may have to un-check resources every time you are exporting.
Check-box the src directory which unfortunately makes it appear you want to export source but actually you need to check-box it in order to export the corresponding .class files.
Check-box Export generated class files and resources.
Browse to the destination JAR file that you want to export or overwrite.
Click finish.
Eclipse does not consider third-party JAR files to be resources in the above step 2 so you need to find a way to provide them to the -classpath or -cp when you invoke Java. The reason we need to do this is because when Eclipse exports a "JAR file" it doesn't seem to follow the build paths to your third-party JAR files. This is a capability that Eclipse has when it exports a "Runnable JAR file" but that's not what we are doing here. You can manually create a directory of your third-party JAR files and let Java expand a wildcard in -classpath.
Example Java invocation in linux:
java -cp ~/directory/destination.jar:/home/username/directory/thirdparty/"*" com.domain.package.MyObject arg1 arg2 arg3
Note that on linux we can allow bash to expand the ~ for your own JAR file, "destination.jar". For the third-party JAR files the long form /home/username is used because tilde expansion might not work in the middle of the string, just after the colon.
If we want to use the Java * wildcard -- not the same thing as the bash command line wildcard -- for the third-party JAR files, the * character must be quoted (made literal) in order for the wildcard to be passed to Java.
Exporting this way eliminates the need to select a run configuration and addresses the concern that prompted the question.
Aside: If your own JAR is exported to the same directory as the third-party JARs then classpath is simpler:
java -cp ~/singleDirectory/"*" com.domain.package.MyObject arg1 arg2 arg3

how to reference maven properties in other files

I have a simple problem (maybe not that simple...) for which I couldn't find a solution. I have created a maven project in eclipse and set a line in the pom like this
<name>myTestProject</name>
When I build the project, a myTestProject.jar is correctly generated. Plus, I want my jar to have input parameters, so I created in the project a .bat and a .sh with the line
"java -jar myTestProject.jar input1 input2 ..."
Now, if I want my target name to change along time, in order not to modify the .bat and .sh each time I have changed the lines according to the maven references like this:
"java -jar ${project.name}.jar input1 input2 ..."
But this does not work; the files are just copied but not processed on compilation time, hence neither the .bat nor the .sh will run the jar.
Is there a way to make maven process these files and change the tags for the correct values when compiling?
Thanks in advance,
Miguel.

How do I get Junit 4.10 to run from the command line on Windows without typing the path to every file?

I guess this is easy for everyone else but I cannot get junit to work from the command line. I had no problem installing the java run time environment, but this junit install has more to it that I'm apparently not seeing.
My class path is:
.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip.;set classpath=%classpath%;C:Program Files\JUnit\junit-4.6.jar;C:\Program Files\JUnit\
My windows path is:
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\wbem;C:\Program Files\Common Files\Teleca Shared;C:\Program Files\Java\jdk1.6.0_30\bin;C:\Program Files\JUnit\junit-4.10.jar;C:\Program Files\QuickTime\QTSystem\
Someone please tell me what I'm doing wrong. I can't get junit to run its test files or any file that I've written.
You should not be putting stuff in the JRE lib/ext directory. Learn how to use CLASSPATH properly.
If that CLASSPATH is an environment variable, you're learning something important: You don't need it. The JVM, IDEs, and app servers all ignore it.
The right way is to use the -classpath option on javac.exe and java.exe when you compile and run.
You shouldn't be altering the PATH for every Java project, either. Point it to the JDK/bin so you can access the tools you need, but that's all you should have to do.

How can I execute a script inside an Eclipse Plugin?

I have one lib/ folder inside my Eclipse Plugin Project and on this folder I have a little script... But when I run the 'Eclipse Plugin Project' I cannot execute that script because I cannot access to that folder...
How can I fix this?
--
Thanks in advance
If you can execute the script by passing an InputStream or a String to the interpreter, put in the src/ folder, so it ends up on the classpath and use getClass().getClassLoader().getResourceAsStream("script-name") to get an InputStream
If the interpreter is external (like bash or something that doesn't implement the Java Scripting API), do the same. When you need to execute the script, create a stream and copy the script to a temporary file.
Keeping the script in lib/ is also a bad idea since the plugin will be assembled into a single JAR file unless you turn that off, so you will end up with a script file inside of a JAR - again something which most interpreters can't use.
By using the classpath, you can let Eclipse figure out where the data is.

What to do in build.xml to add parameters in java command in the execution of the program?

I can only execute an application i made, with the following command in the command line:
java -jar -Djava.net.preferIPv4Stack=true P2P_network.jar
I would like to know, how can i put this in the jar file, so i can execute it automatically, without needing to go to the command line.
The IDE used is netbeans.
This answer may help. The 'dash dee' and 'dash enableassertions' are both VM Options.