Use Java to start a Windows exe - java

I want to find out how to open any exe in Windows using Java code. I have searched Google before and they only show me part of the code that they use, I think, because it doesn't seem to compile.
I have downloaded JDK 7 to compile. I don't use Eclipse at the moment and also explaining what I had to do to get it to work in detail would help a lot.
to what Sri Harsha Chilakapati said: would i need to create a class for the code?
Thanks to those who answered but i didn't quite get what you meant but i did however manage to find a website which had what i was after:
http://www.rgagnon.com/javadetails/java-0014.html
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(
"\"c:/program files/windows/notepad.exe\"");
p.waitFor();
}
}
the above was what i was after but thanks again anyway to the people who answered.

Try this.
String myExe = "C:\\MyExe.exe";
String args = "";
Runtime.getRuntime().exec(myExe + " " + args);
Hope this helps.

I would recommend the ProcessBuilder, especially for additional arguments.

Related

Visual studio code not showing output, java programming

I'm learning to code Java...
I just installed the Java runtime environment and Visual Studio Code, and wrote this:
public class IterationDemo
{
public static void main(String[] args)
{
System.out.println('x');
}
}
However, I don't see any output, both under output or terminal. Only kind of a rectangle (▯) under terminal.
I've tried to find a solution googling, stack and so on but no answer yet...
what can I do?
Thanks!
OK; got solved on its own - I ended up clearing the cache and then the output came :)
Thanks myself!

Running a python code using process builder Java

I need some help.
I am trying to run a python script called mantime.py from a directory. I've tried to google it and found several ways to do it. Yet, I still got 2 as the exit value, which I expect it 0 (terminate normally). Here is my code:
public int performedManTime() throws IOException, InterruptedException{
ProcessBuilder pb = new ProcessBuilder("/usr/bin/python","/Users/ab/Downloads/ManTIME/mantime.py","-ppp","test",inputDir.getAbsolutePath(),"i2b2");
Map<String,String>env = pb.environment();
env.put("MANTIME_CRF_TRAIN", "/usr/local/Cellar/crf++/0.58/bin/crf_learn");
env.put("MANTIME_CRF_TEST", "/usr/local/Cellar/crf++/0.58/bin/crf_test");
env.put("MANTIME_CORENLP_FOLDER","/Users/ab/Downloads/ManTIME/externals/stanford-corenlp-full-2014-08-27");
Process process = pb.start();
process.waitFor();
System.out.println("Exit Value: "+process.exitValue());
return process.exitValue();
}
-ppp, test, input.dir and i2b2 are the arguments for the mantime.py
I tried to set up the environment as we can see above. Does anyone knows what are the problems? Any comment or suggestion would be really appreciated. Thank you
EDIT: I suspect since the python is on different directory with my tool (/usr/local/python). After I put the code bellow, somehow it works.
ProcessBuilder("/usr/bin/python","/Users/ab/Downloads/ManTIME/mantime.py","-ppp","test",inputDir.getAbsolutePath(),"i2b2");
pb.directory(/myToolsDir)
I suspect since the python is on different directory with my tool (/usr/local/python). After I put the code bellow, somehow it works. Thank you guys
ProcessBuilder("/usr/bin/python","/Users/ab/Downloads/ManTIME/mantime.py","-ppp","test",inputDir.getAbsolutePath(),"i2b2");
pb.directory(/myToolsDir)

Is there a way to add code to make the dev of a plugin have all of the commands by default?

I have made a few MC plugins, and would like to know if this is possible.
If so, would you mind helping me a bit? If I need to provide more info, please don't hesitate to ask <3
You could always add a manual check to your commmand code when checking the permission, like so:
private static String DEVELOPER = "user3262369";
public void doMyCommand(Player player)
{
if(player.hasPermission("mymod.permission.name") ||
player.getName().equals(DEVELOPER))
{
//Do whatever your command does here
}
}
For more information, see the Permissions section of the Bukkit Plugin tutorial and the Bukkit org.bukkit.entity.Player API.

Google Caliper: Trouble getting started (Java Benchmarking)

I am trying to use Google Caliper to benchmark some simple code. I am using the examples from their websites. Here's what I've done so far:
Downloaded the Caliper JAR and added it to my Netbeans project
After having difficulties, I downloaded JUnit.jar and hamcrest.jar. Still not working.
Here's my code:
import com.google.caliper.Benchmark;
public class Benchmark1 extends Benchmark {
public void timeNanoTime(int reps) {
for (int i = 0; i < reps; i++) {
System.nanoTime();
}
}
}
I am extending Benchmark because when I try to extend "SimpleBenchmark" like on their website it tells me it cannot find SimpleBenchmark. I then, in my main method, create a new Benchmark1() hoping something will happen. Nothing does. This is the code inside my main class.
Benchmark1 test = new Benchmark1();
test.timeNanoTime(10);
I know this is no doubt a simple error but I cannot, despite much Googling, figure out where I'm going wrong. The code compiles but does not run.
EDIT: I should say I'm running Netbeans on Windows 7 with Caliper 1.0
It's true; the documentation is woefully outdated and incomplete. I'm working on it. In the meantime, here's what will get your benchmark running.
Your main method should delegate to CaliperMain, not directly to the benchmark. Try
public static void main(String[] args) {
CaliperMain.main(Benchmark1.class, args);
}
Windows will be a problem. Particularly, issue 215 will be the biggest blocker.
You could switch over to Perfidix http://perfidix.org/
Perfidix has eclipse Integration and can be used like JUnit.
Another option would be JUnitbenchmarks http://labs.carrotsearch.com/junit-benchmarks.html
It's a really great framework for Junit 4+. It can even build html charts to compare results.

How to obtain OS directory

I,m looking for some method that can let me obtain (in windows) the directory where windows is saved (for example in my PC it will return "C:\windows".
I need it because I have to call this method
public static void openFileWithNotepad(String pathFileTxt) throws InterruptedException, IOException
{
if(System.getProperty("os.name").toUpperCase().contains("Windows".toUpperCase()))
{
String program = "C:/WINDOWS/system32/notepad.exe";
Process p = Runtime.getRuntime().exec(program + " " + pathFileTxt);
}
...
}
I want to use some method to switch "C:/WINDOWS" with the OS installation folder, in order to use this program on different pcs.
P.S.: If someone know, I'd like also to know how to use this method on UNIX OSs :)
Thank you for understanding!
Desktop.getDesktop().open(new File(pathFileTxt));
Works for any file for which there is an associated program, on any OS that supports Java 1.6+. See Desktop.open(File) for details.
i believe this should work:
System.getenv("WINDIR")
also, notepad doesn't tend to exist on unix, so i'm not sure where you are going with that...
try
System.getenv("windir")
for windows.
I'm not sure about other OSs.
System.getenv("WINDIR") may work for you.

Categories