Processbuilder-opening java script in linux - java

Back again, this time with a java question. I was told how to get Processbuilder to run nonnative scripts (calling the program that would run the script), but I've been trying to run a java script and have run into a couple problems. First off, should I use a .class or .jar? both of these can be run but I'm not sure which one of them will work better. And then how do I execute them correctly? I've tried calling java (/usr/lib/jvm/java-6-openjdk/jre/bin/java) and then giving the filepath to the class file, but that doesn't seem to work.
Any ideas?

I agree with sarnold in terms of the .jar question. In terms of executing code using ProcessBuilder, you can execute a .jar file as long as this file contains a main() method, and has the Main-Class manifest header, which can be generated when the .jar is created. Once you have the .jar created, you'd use a command like this to run the .jar:
java -jar jar_file_name_here.jar
If you have multiple main classes and you want to run a specific one, you could use a command like this:
java -jar jar_name.jar com.main.class.package.path.here.SomeClassName
Are you trying to execute someone elses .jar, or is it one of your own that you just want to be executed inside a script? Why are you using a script, out of curiosity?

You either need -jar and the pathname of a jar that has a manifest that names your main class, or -cp with the pathname of a directory that has your classes in it in the standard layout, or -cp with the pathname of a jar followed by the name of the class with a main.
java -jar I_AM_A_JAR_WITH_A_MANIFEST.jar
java -cp I_AM_JAR_1.jar:I_AM_JAR2.jar... this.is.my.FooClass
java -cp dir_path1:dir_path2:dir_path3 this.is.my.FooClass
where the 'dir_pathN' is a dir with standard class hierarchy.

Related

java Could not find or load main class from eclipse compiled code

I have compiled my java code using eclipse but not it has to be deployed and a cron job has to execute it. I am trying to execute it from command line in Windows, but getting Could not find or load main class. I tried setting classpath using java -cp bin\com\pega\download\engineclasses but it still throws the same error. My folder structure looks like below
C:\Users\s2517457\G360_Linux\FiddlingPega
|__\bin\com\pega\download\engineclasses\TestUtils.class
|__\src\com\pega\download\engineclasses\TestUtils.java
Please let me know what should be the javac and java commands for this to work.
You should use the following command:
java -cp bin/ com.pega.download.engineclasses.TestUtils
Your are telling to java that the entire bin folder is your classpath and the main class is in the class com.pega.download.engineclasses.TestUtils
If you want to add jars as well, you must call the command like:
java --classpath "bin/;lib/*" com.pega.download.engineclasses.TestUtils
Where lib is the folder containing the Jars files

Setting java classpath only when compiling

Is it possible to set the class path only when using javac but then run the program without specifying the class path at java?
For example,
I am using ant that does the compiling for me and it sets the class path to ./:lib/swingx-all-1.6.3.jar. As far as I know, this basically compiles/uses javac with that class path.
So is it possible to then run my program with just java MainApp, i.e. without having to use a class path variable? Or is it only possible to run my program by using the ant command again?
You can bundle your application in a jar file. Then include your classpath in manifest.
Here are Ant manuals for jar and manifest tasks.
Then, to run your application:
$ java -jar MainApp.jar
If you can use ant to run your program, look at the java task. Create a new target named "run" or something like that.
You don't need to set the classpath environment variable; you can always supply a classpath manually with the -cp option.

I need a solution to starting Slick 2D/LWJGL Java project via command line. Help?

Currently I am starting my Slick 2D application via this batch file-
java -Djava.library.path=lib -Xms512m -Xmx512m -jar myapp.jar %1
Where lib is the folder that contains the LWJGL/Slick libraries and myapp.jar is my application.
Although this is easy and effective I want to be able not to have to run a script all the time and actually create a java .jar to do this.
So my question is, how would I 'convert' this batch script into Java code?
Why you would want Java code for this is beyond me as that creates exactly the same problem for you again – namely running your startup Java program that then starts another Java program; you'd be at the same point as before.
However, you don't need to create a JAR in any case. You can stuff all your compiled .class files somewhere and set that as the classpath. A JAR is little more than a main class and a classpath bundled into one.
So instead of your invocation above you can then use
java ... -cp %USERPROFILE%\Java\MyApp myapp.gui.Main
or something like that. Set the classpath with -cp and give the main class on the command line instead of the JAR.
Any -D command line arguments can be set via java.lang.System.setProperty. But the memory parameters can't be set from inside a JVM as far as I know. Therefore, there is no way to do what you want.
Instead you could generate e.g. a Windows executable with JSmooth. Such a wrapper should be able to set all JVM arguments. But in the end the situation is similar to the script. You have some kind of wrapper.
The easiest way is to use a program like JarSplice (http://ninjacave.com/jarsplice)
You can easily create a jar executable with all needed lib. it works very well

Simple Classpath question using with jars

I have a toy program that is called Test.class. It accesses a class in a jar called myjar.jar. That jar is in my CLASSPATH variable set as part of my Windows environment. When I type echo %CLASSPATH%, I see C:\myclasses\myjar.jar. When I execute my program
java Test
it runs fine.
But if I package the code as a jar and try running
java -jar Test.jar
It ca
It can't find my classpath. I know this has a simple solution.
Can you please help me.
When -jar (or -cp or -classpath) argument is been used, then the %CLASSPATH% will be ignored. Instead, the Class-Path entry in JAR's /META-INF/MANIFEST.MF file will be used. You'd like to put the JAR-relative path to the other JAR in there. E.g.
Class-Path: myjar.jar
The above example expects the myjar.jar to be in same folder as the JAR file you'd like to execute.
An alternative is to package the 3rd party JAR inside your JAR file. In for example Eclipse you can do this.
First off I would not bother with this stuff myself anymore(I used too) and let my IDE(Netbeans/Eclipse) figure this stuff for you out. BTW I already hope you are using an IDE because it makes programming in Java that much more fun.
Next I would advice to learn a build tool like maven.

System.out not working when calling jar-file from Windows command line

I have this class:
public class Test {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
And I used Eclipse's "Export to runnable JAR" function to make it into a executable jar-file (test.jar). When I run this program in Eclipse, it prints out "Hello World!", but when I call the jar-file from the command line, nothing happens.
I have tried calling the jar-file like this:
test.jar
test.jar -jar
There is no output whatsoever.
I have another program that also has a side effect (in addition to output to stdout) and while the side effect is being performed (which tells me the jar-file was definitely executed), again no output is given to the command line. I have also tried using stderr, but that makes no difference. Does anybody know how I can make this work?
Thanks!
You must run the JAR using
java -jar test.jar
(Your JRE's bin folder must be added to PATH in order to get the command working from any location)
NOTE: I know you created the JAR using Eclipse but you might want to know how does an executable JAR works
The previous answers are correct, so I'll just clarify a bit the "executable jar" concept.
There's no such thing as an "executable jar", equivalent to an exe file in windows, in an "executable jar" you'll be only specifying the "entry" point ( your main class you want to be executed by default )
A jar is basically just an archive, you'll still need java to actually launch the jar ( java -jar your.jar )
The fact that in windows you might have an association with javaw.exe means this will be launched by the OS when you double-click on the jar, like a .txt file being opened automatically with notepad, it doesn't make the .txt an executable file.
Check this out as well :
JAR files revealed
When you invoke the jar using 'test.jar' the starting of the app is handed off to the registered java handler for jar files, which doesn't run in the context of the command line.
The default jar handler doesn't open console based System.{out,err} file handles, as it would mean a cmd style window for each of the jar files launched, which is not an ideal situation.
The previous answer, using java -jar test.jar causes it to run within the context of the current cmd window, and thus you will see the output.

Categories

Resources