I am indebted always.
How can I do to start the external program of java, but it does not work.
In the following program, although I will work if started from the command line,but,It does not work when i run in eclipse on.
I am out error that appears is, and can not load the main class is missing or(エラー:メインクラスがみつからないかロードできません).
How will be able to run an external program in eclipse on what if?
Please kindly advise Thank you.
package test.jersey.resources;
public class Execute02 {
public static void main(String[] args) {
String filename = "C:\\Users\\omiz12032\\workspace3\\TestJavaServlet\\src\\test\\jersey\\resources\\start.bat";
try {
Process proc = Runtime.getRuntime().exec(filename);
System.out.println("実行中");
proc.waitFor();
System.out.println("実行終了");
} catch( Exception e ) {
System.out.println(e);
}
}
}
You're calling Runtime.exec(String) which delegates to Runtime.exec(String[], String[], File) in two steps, result in nulls for second and third parameters.
Those are envp and dir. Inheriting envp (aka Environment Variables) from your Java process shouldn't be a problem. The bigger problem is dir which is the working directory. If the .bat file you're calling is not well-written (not ready for being executed outside its directory) it'll fail to resolve its dependencies when using your program's working directory. Try the following:
File workingDir = new File("C:\\Users\\omiz12032\\workspace3\\TestJavaServlet\\src\\test\\jersey\\resources");
// the no-array call of exec tokenizes your command which may lead to unexpected results if it contains spaces
// also you can put arguments here as well as separate array elements
String[] command = { "start.bat" }; // for example { "start.bat", "--fooBar" }
Process proc = Runtime.getRuntime().exec(command, null, workingDir);
Related
I am trying to start a new process with Runtime.exec() from my javafx application.
The new process is my javafx application (but in a new process, the "parent" one will still be open).
So I run javaw via the exec method and tell it my classpath. And here is my problem: the classpath contains whitespaces, so I need to quote every path. But I retrieve the path at runtime via java.class.path (since it is the same application).
Do I need to process the string and quote everything or is there an easy way to get this to work?
Here is the code:
public static void startInNewProcess() {
try {
Runtime r = Runtime.getRuntime();
File javaPath = new File(System.getProperty("java.home"), "bin/javaw");
File classPath = new File(System.getProperty("java.class.path"));
System.out.println("java loc: " + javaPath.toString());
System.out.println("classpath: " + classPath);
Process p = r.exec(javaPath.toString() + " -classpath " + classPath.getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
I get the following string as classpath:
classpath: C:\Users\kwilhelm\git\ResourcePlaner\bin;C:\Program Files (x86)\eclipse\plugins\org.eclipse.fx.ide.css.jfx8_2.0.0.201506111511.jar;C:\Users\kwilhelm\git\ResourcePlaner\lib\itextpdf-5.5.6-javadoc.jar;C:\Users\kwilhelm\git\ResourcePlaner\lib\itextpdf-5.5.6-sources.jar;C:\Users\kwilhelm\git\ResourcePlaner\lib\itextpdf-5.5.6.jar;C:\Users\kwilhelm\git\ResourcePlaner\lib\controlsfx-8.40.9.jar
But javaw gives the error that it can't find mainclass "Files", so it can't handle the whitespace in the path.
So is there a way to get the classpath with quotes?
Is there a better solution?
Any help is apreciated
And here is my problem: the classpath contains whitespaces, so I need to quote every path.
Actually, no you don't.
And in fact, if you do attempt to quote every path, it is likely to mess up bigtime, because exec doesn't understand shell quoting.
What you actually need to do is this:
Process p = r.exec(new String[] {javaPath.toString(),
"-classpath",
classPath.getPath()});
This tells exec exactly where the boundaries of the command arguments are, so that it doesn't need to try (and fail) to figure it out for itself.
In fact, that still isn't right. You also need to add:
any other JVM options that the cloned instance needs,
a class name, and
any arguments required after the classname.
The classname is mandatory. (You left it out, and that is why the java command was outputting its help message!)
I ran into library loading problems after creating a jar from my code via maven. I use intelliJ idea on Ubuntu. I broke the problem down to this situation:
Calling the following code from within idea it prints the path correctly.
package com.myproject;
public class Starter {
public static void main(String[] args) {
File classpathRoot = new File(Starter.class.getResource("/").getPath());
System.out.println(classpathRoot.getPath());
}
}
Output is:
/home/ted/java/myproject/target/classes
When I called mvn install and try to run it from command line using the following command I'm getting a NullPointerException since class.getResource() returns null:
cd /home/ted/java/myproject/target/
java -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter
same for calling:
cd /home/ted/java/myproject/target/
java -Djava.library.path=. -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter
It doesn't matter if I use class.getClassLoader().getRessource("") instead. Same problem when accessing single files inside of the target directory instead via class.getClassLoader().getRessource("file.txt").
I want to use this way to load native files in the same directory (not from inside the jar). What's wrong with my approach?
The classpath loading mechanism in the JVM is highly extensible, so it's often hard to guarantee a single method that would work in all cases. e.g. What works in your IDE may not work when running in a container because your IDE and your container probably have highly specialized class loaders with different requirements.
You could take a two tiered approach. If the method above fails, you could get the classpath from the system properties, and scan it for the jar file you're interested in and then extract the directory from that entry.
e.g.
public static void main(String[] args) {
File f = findJarLocation("jaxb-impl.jar");
System.out.println(f);
}
public static File findJarLocation(String entryName) {
String pathSep = System.getProperty("path.separator");
String[] pathEntries = System.getProperty("java.class.path").split(pathSep);
for(String entry : pathEntries) {
File f = new File(entry);
if(f.getName().equals(entryName)) {
return f.getParentFile();
}
}
return null;
}
I'm in a situation where I need to launch a .exe, but I meed to launch it with parameters, AND it needs to be launched as an argument itself, but cant be launched from a command line. I need to launch javaw itself, with my exe as an argument. For example:
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "PATH/TO/.exe"
However, "PATH/TO/.exe" needs -arg1 and -arg2. Placing the parameters inside of the path leads to java not finding them, whereas placing them outside leads to java thinking they belong to itself and trying to use them.
EDIT: I am running the exe with java because I am using a completely different program to start all this. I cannot use a different java program, all I have to work with is the target box. It is designed to start exe's, but I need to run the exe through java. ALL I have to use is the path area where you would usually type where the exe is located.
The basic trick is:
try {
final Process p = Runtime.getRuntime().exec(ARRAY_OF_EXE_AND_ARGUMENTS);
if (p == null) {
// some error
// probably command not found, abort.
}
try {
final int retval = p.exitValue();
if (retval == 0) {
// Process ended immediately.
// Probably a problem, abort.
} else {
// Process crashed, abort.
}
} catch (final IllegalThreadStateException itse) {
// Process is running.
// all is good
}
} catch (final IOException e) {
// Error running exe, abort.
}
Where ARRAY_OF_EXE_AND_ARGUMENTS is a String array, where first is path to the executable, and following are it's command-line arguments.
To pass the exe and argumetns to java, this is the common pattern:
java -jar MyJar.jar "/path/to/virus.exe argument1 argument2"
And then you split it by space or something.
I have been trying to get this program to run a class file that is located in /Library/Application Support/Adobe/Adobe PCD/cache/Main.class.
Yet every time I run the method below, It cannot find the file specified. I think that eclipse is looking for the file path above in the project folder. But the first command doesn't seem to work. Can Anyone help me? Is there another way to run the class file?
static Runtime r = Runtime.getRuntime();
public static void Run() {
try {
Runtime rt = Runtime.getRuntime();
String run = "/Library/Application Support/Adobe/Adobe PCD/cache/";
String[] command = {"java -cp " + run, "java Main"};
Process pr = rt.exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
This tries to run a mythical program java -cp /Library/Application Support/Adobe/Adobe PCD/cache/ with argument java Main. java Main is an illegal name for a class too. Instead, have your command array be:
String[] command = {"java", "-cp", run, "Main"};
However, unless you have a Main class in the default package, simply add the run directory to your application's class path, and then call Main.main(), possibly playing games with standard input and output. There is no need to start a second JVM. Or, figure out what Main does, and call the classes it calls. Is there Javadoc for the Adobe code?
When the Ant task <zip> runs, Ant does not start a new VM or a command-line zip utility. Instead, the task's class calls methods in java.util.zip. You should do something similar.
I tried out a simple program to execute Linux command at run time. But the following program gets compiled and runs without any error, but the text file is not getting created as intended.Is there anything wrong in this program?
import java.io.*;
class ExecuteJava
{
public static void main(String args[])
{
String historycmd = "cat ~/.bash_history >> Documents/history.txt";
try
{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(historycmd);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Try accessing some of the functions Process provides. I'd start with exitValue. Typically a -1 indicates something went wrong while a 0 means nothing especially bad happened.
Also try InputStream and Error Stream, and read them fully. See if either has useful feedback for you.
Other than that, try what andy256 suggests in comments. Ensure the Documents directory exists in the executing directory of the program.
The append operator >> is meant to be interpreted as part of the command shell. Use
String[] historycmd =
{ "bash", "-c", "cat ~/.bash_history >> Documents/history.txt"};