I'm making a web application that, at a certain point, starts a new thread and this thread executes a jar file from command line.
The command line jar works fine when called from outside the application, but when i call it from the thread the relative path becomes C:\eclipse\ (i'm running the application from Eclipse) instead of the directory it's stored in, which messes up with its configuration since it looks for files in the wrong place.
The jar creates a log file, whenever i try to call it i have this line written in the log: "10/04/2012 17:09:03 - java.io.FileNotFoundException: C:\eclipse\descriptors\analysis_engine\AggregateAE.xml"
The jar is not inside C:\eclipse. When i call it from prompt i have no problems, but when it's called from a new spawned thread i have this error. I've tried it on a production environment and i have the same problem (this time the base path is the server's one)
Considering that i can't modify all the paths, what could be a solution to this problem?
EDIT: this is the thread class that calls the jar
public class UimaThread extends Thread {
private int mode=0;
private String path;
public UimaThread(int mode, String path){
this.mode=mode;
this.path=path;
}
public void run() {
Runtime run = Runtime.getRuntime();
try {
Properties config = ConfigLoader.getConfig();
String uimaPath=config.getProperty("uimaPath")+ControlPanelUtils.getDelimiter(config.getProperty("uimaPath"));
//uimaPath is the absolute path to the jar file, mode and path are just arguments passed to the jar
run.exec("java -jar "+uimaPath+"uimachainfull.jar "+mode+" "+path);
}
}
The code running this is:
public void startUima() throws IOException, ServletException {
Properties config = ConfigLoader.getConfig();
UimaThread uimaThread = new UimaThread(2, config.getProperty("docPath"));
uimaThread.start();
}
I need this to be executed asyncronously and outside the server, i've asked how to do that here in stackoverflow and i've been told to do so: Calling an application from a web server asynchronously
I've discovered how to do it, instead of using exec(String command) i had to use exec(String command, String[] envp, File dir) and the last argument (dir) is the working directory that can be passed as new argument
Related
I have created VBS script and am calling it in my Java program. This was working flawlessly until I changed the path.
I see the below error:
Things I did:
I changed the path in my VBS script
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("GALE-GoEasy-QA\GoEasy\requiredSource\TestData1.xlsm")
objExcel.Application.Run "TestData1.xlsm!refreshXLS"
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Quit
My Java program calls this function to execute the program
public void refreshExcelData() throws IOException {
Runtime.getRuntime().exec(new String[] {
"C:\\Windows\\System32\\wscript.exe",
"myVBS.vbs"
});
}
Also I have attached a picture of my project folder structure
PS: I wanted to use relative path instead of absolute
Try the below code:
public void refreshExcelData() throws IOException {
Runtime.getRuntime().exec(new String[] {
"C:\\Windows\\System32\\wscript.exe",
System.getProperty("user.dir")+"\\requiredSource\\myVBS.vbs"
});
}
I am writing an eclipse-plugin witch run program.exe. I have added program.exe to plugin jar file. How can a execute this program?
public class Handler extends AbstractHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
Runtime.getRuntime().exec(/*What should I write here*/);
return null;
}
}
You can't run the program.exe from inside the plugin jar, so it needs to be extracted. In your plugin use:
Bundle bundle = Platform.getBundle("plugin id");
URL url = FileLocator.find(bundle, new Path("relative path to program"), null);
url = FileLocator.toFileURL(url);
This will find the program in the plugin jar and extract it to a temporary location (done by FileLocator.toFileURL).
You should just execute the program like you would in cmd, but now specify the whole path of the programs location.
Runtime.getRuntime().exec("C:\\your\\path\\program.exe");
In the Oracle documentation of the Runtime class you can see the acceptable inputs in exec().
public static void main(String args[]) throws IOException
{
Process p = Runtime.getRuntime().exec("java E:/workspace/JNIProgram/src/JNIProgram.class");
}
so I have this code and am trying to run the JNIProgram.class file however the program gets terminated instantly without doing its job (which is to create a new txt file and write to it)
So what am I doing wrong
The java command expects a Java class name, not a filename.
So the command java E:/workspace/JNIProgram/src/JNIProgram.class is wrong. If you try this manually from a command prompt window you'll get an error message.
The command should be something like this:
java -cp E:\workspace\JNIProgram\src JNIProgram
Note: What's after the -cp option is the classpath, and after that the fully-qualified class name (which is just JNIProgram, if the class is not in a package).
First make sure that you can run the command manually from the command line before you make it work from another Java program.
I'm having problems using ProcessBuilder to run a class in my project.
My code:
public class Main {
public static void main(String[] args) {
try {
String pathToJar = Main.class.getProtectionDomain().getCodeSource()
.getLocation().toURI().getPath();
ArrayList<String> params = new ArrayList<String>();
params.add("javaw");
params.add("-classpath");
params.add(pathToJar);
params.add("Program");
ProcessBuilder pb = new ProcessBuilder(params);
Process process = pb.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Program is in same project (same bin folder) and works fine if ran directly but this way I get the error "Could not find the main class: Program". Where is the error in my code?
Thanks in advance.
[EDIT]
I came to the conclution that is some code on my Program class giving error. Basicly only runs with "clean" main. At eclipse, Program class is importing some libraries that are inside a jar file. Don't I need to reference it in ProcessBuilder? If so, how?
In response to your edit:
You can add the current path by switching params.add(pathToJar); with params.add(System.getProperty("java.class.path").concat(";").concat(pathToJar));.
Where is the error in my code?
(You are launching the javaw executable, so that is not the problem. It is also not that your entry point method's signature is incorrect, because that would have given a different diagnostic.)
The problem is either that the class name is incorrect (e.g. if should be "come.pkg.Program"), or the pathname for the JAR file is incorrect.
Assuming that you have eliminated the possibility that the class name is incorrect, my guess is that you are trying to use a relative pathname for the JAR file, but there is some confusion over what the current directory is; i.e. the directory in which the pathname needs to be resolved. Try using an absolute pathname in the classpath parameter.
How to trigger a jar working on Hadoop from a simple jar, so that it uses HDFS, Actully, I am manually running this command bin/hadoop jar ~/wordcount_classes/word.jar org.myorg.WordCount ~/hadoop-0.20.203.0/input1 ~/hadoop-0.20.203/output2 in which I have provided Input and Output directory in HDFS and I am using word.jar here, I want to make it such that it automatically gets triggered from Java Project.
In best of my understanding all you asking for is done by the Main of your jar. It read parameters, creates job configuration, sets input and output formats and finally runs the job.
I'm working on the same problem. I have a program (let's call it Driver) that must implement the following method:
public void runJar(File jar, String mainClass, File inputDir, File outputDir);
To do this, I was calling org.apache.hadoop.util.RunJar.main(String[]) which is what your command-line is calling. This works great only if you're running Driver from the command line.
If Driver is running inside a container (like Tomcat or Jetty), you're going to have a problem. You'll get errors like
java.lang.ClassNotFoundException: org.apache.hadoop.fs.Path
This is because of how RunJar messes with classloaders. You need to manually create a classloader like so:
final ClassLoader original = Thread.currentThread().getContextClassLoader();
try {
URL[] urls = new URL[] { jar.toURI().toURL() };
ClassLoader loader = new URLClassLoader(urls, originalLoader);
Thread.currentThread().setContextClassLoader(loader);
Class<?> mainClass = Class.forName(driverClass, true, loader);
Class[] argTypes = new Class[]{ Array.newInstance(String.class, 0).getClass()};
Method main = mainClass.getMethod("main", argTypes);
main.invoke(null, new Object[] { args });
} finally {
Thread.currentThread().setContextClassLoader(original);
}