here i'm trying to launch a java program inside another java program.
after some search in the forum i found some clues but my code launches only .exe file and not .java file why?
import java.io.*;
public class Mana
{
public static void main(String args[])
throws IOException, InterruptedException
{
try
{
Process p=Runtime.getRuntime().exec(" D:\\NetBeansProjects\\GetIPAddress\\dist\\GetIPAddress.jar");
}
catch(IOException e1) {System.out.println(e1);}
}
}
for java file you must run javaw.exe(not java, because java.exe shows new console window) from jre:
Process p=Runtime.getRuntime().exec("javaw -jar D:\\NetBeansProjects\\GetIPAddress\\dist\\GetIPAddress.jar");
If you are ok to use 3rd party library, you can use:
http://code.google.com/p/jlibs/wiki/JavaProcessBuilder
Okay, looks like you have several problems.
First, to call a java program as a jar file, you need to have the jar file. Your question first looked like you already had one (producted by Netbeans). So try this:
java -jar D:\NetBeansProjects\GetIPAddress\dist\GetIPAddress.jar
in a console window (cmd.exe on Windows). If this works, then your call as given by Sergey should work too. If it does not work, then you have to look at how to create this jar file.
Related
Im having trouble with using Desktop.getDesktop().open(). Im trying to run one java file from another one, using this code:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class OpenFile {
public static void main(String[] args){
String program = "HelloWorld.java";
try {
Desktop.getDesktop().open(new File(program));
} catch(IOException e) {
System.out.println(program + " if not on the desktop");
}
}
}
I have a file called HelloWorld.java sitting on my desktop, but I'm getting this error:
Exception in thread "main" java.lang.IllegalArgumentException: The file: HelloWorld.java doesn't exist.
at java.awt.Desktop.checkFileValidation(Desktop.java:210)
at java.awt.Desktop.open(Desktop.java:270)
at OpenFile.main(OpenFile.java:10)
I'm on a Mac, so that may be the problem but I'm not sure. I'd appreciate any advice!
There are two issues that I see here.
You want to "run" a Java program, which is not the same as "opening" a Java file.
Even if you are trying to "open" a Java file using Desktop.getDesktop().open, this does not mean that getDesktop().open points to your "Desktop" folder from where you can open a file on your Mac's Desktop.
To execute your Java program, you need to first compile the Java program using the Java compiler. Then you would be able to "run" your program.
Runtime.getRuntime().exec("javac /Users/username/Desktop/HelloWorld.java");
Runtime.getRuntime().exec("java -classpath /Users/username/Desktop HelloWorld");
The Desktop.getDesktop().open(File file) method simply opens the selected file in the default application for that file type. For example, on a Mac, a text file would be opened by TextEdit.app.
Hope this helps!
So I made an executable file with Eclipse but when I double-click it nothing happens. It IS NOT because I've got wrong association for running .jar files (I'm running okey my another jar program just by double-clicking it).
I also tried with command prompt and the console doesn't print anything at all and get back to be "normal".
I think it's something with main class so the thread doesn't last but closes after running (however it looks like the code isn't run because console would print lot of Slick stuff).
So here is the main method:
public static void main(String[] args) {
Shiftjumper shiftjumper = new Shiftjumper("Shiftjumper - behavior prototype");
try {
AppGameContainer gc = new AppGameContainer(shiftjumper);
gc.setDisplayMode(TILE_SIZE*RIGHT_LEFT_TILES, TILE_SIZE*UP_DOWN_TILES, false);
//gc.setTargetFrameRate(60);
gc.start();
} catch (SlickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And here the manifest:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ lwjgl.jar slick.jar
Class-Path: .
Rsrc-Main-Class: engine.Shiftjumper
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
You need to download a tool known as JarSplice. JarSplice is a very simple GUI based application which packages all libraries and native code into the form of either a .JAR, a .EXE, a.SH or a .APP. Here is a link to the site: http://ninjacave.com/jarsplice
Hope this helps :)
Try running the jar in your terminal or command prompt, if there are any exceptions it will print to the console and you can debug
I made a little program and it worked fine, but now. First, it mux the xml chapter file in the mkv file, so we get a muxed mkv file. Some day ago I updated java to 1.7.21 and I think this is the problem why it is not working now. It's a little strange, but when I run in netbeans everything is fine, but when I build and I run the .jar file, it is not working. It create the xml file, but not mux in the mkv file (and because not muxed not delete the xml file). Here is the code: (filename=xml file path; mkv=mkv file path)
public void muxing() {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("c:\\Program Files\\MKVtoolnix\\mkvpropedit.exe --chapters \""+filename+"\" \""+mkv+"\"");
if (p.waitFor()==0) {
File xmlfile=new File(filename);
xmlfile.delete();
}
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
The program worked with java 1.6 and I think with 1.7.17 too. Win7 32bit. Sorry for my bad English.
Oracle has made breaking changes to Runtime.exec() in Java 7 update 21 (and 6 update 45).
If the program name contains spaces, you need to specify command and arguments in an array:
Process p = Runtime.getRuntime().exec(new String[] {
"C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\""});
Another option is to use java.lang.ProcessBuilder:
Process p = new ProcessBuilder("C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\"").start();
As stated by Oracle:
Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.
Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.
I have the following problem in php when excecuting a jar file. I use the following command:
exec("java -jar JavaProject4.jar";
The JavaProject4.jar creates a txt file in a path given in the java code.
When i run the project in NetBeans the txt file is created. However, when i excecute the jar in php i don't get any errors but i can't get the file.
Here the java code I use to write the file:
public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException {
Main a = new Main();
List<Double> l1 = new ArrayList<Double>();
l1 = a.compute_features();
//System.out.println(l1);
FileWriter fstream1 = new FileWriter("C:/wamp/www/test/out.txt");
BufferedWriter out1 = new BufferedWriter(fstream1);
out1.write(l1.toString());
out1.newLine();
out1.close();
}
Im using a wamp server with php 5.2.4 and the latest java version.
Thanks a lot!
EDIT:
Problem solved, I moved the main java file in NetBeans to the default package and also fixed a wrong path and now everything is working as expected.
Thanks everyone
When you run it with PHP, how are you doing so? Are you using the PHP CLI (Command Line Interface), or are you running it through an Apache Module (CGI or otherwise)? The reason I ask, is because the problem you are having could have something to do with the user who the script is executing as. If you are using the CLI, you are running as your Windows User, however, if you are running it through Apache, then it is running as whatever user Apache is running as. Therefore, you might need to give the relevant permissions to the Apache user for that directory you are writing to.
Regards,
Ralfe
I think that you need to specify the full path for :
- the java executable
- your jar
Example :
exec("/usr/bin/java -jar /my/java/project/path/JavaProject4.jar");
OK, I just cannot get java to run my .class files:
I follow steps in Oracle tutorial and try to run this program:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Compiling OK:
PS C:\Users\Ztaz> javac .\HelloWorldApp.java
But after I try to run it, I get this:
PS C:\Users\Ztaz> java .\HelloWorldApp.class
Error: Could not find or load main class .\HelloWorldApp.class
no exception, nothing.
Here's my PATH variable, if it helps (split into lines, for readability):
%JBOSS_HOME%;
%SYSTEMROOT%;
%M2%;
%JAVA_HOME%\bin;
...
JAVA_HOME is set to "C:\Program Files\Java\jdk1.7.0". My question sounds a lot like this one but I had this problem on Java SE 6 as well, so I decided to post separate question.
Run it without the .class: java HelloWorldApp
This causes issues for lots of people starting out with Java. Not sure why Java doesn't just look for both files (the name provided and the name with .class appended).