Java Startup Application [WINDOWS] - java

i have this simple code in java that shutdown the pc. What can i add to run this at Windows StartUp?
import java.io.IOException;
import java.io.OutputStream;
public class Spegni {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec("C:\\WINDOWS\\system32\\cmd.exe");
OutputStream os = process.getOutputStream();
os.write("shutdown -s -f -t 0\n\r".getBytes());
os.close();
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

start, run:
shell:startup
and then place the shortcut there. Happy trolling.

Related

How to get coordinates of external application opened by Java program

Using Java program I am opening an external application( for example: notepad). How can i get coordinates/location of external application for screen capture/screen-shots.
I can take screen-shots of the whole window, but not for a particular application.
I have already tried "Robot" for screen capturing but not able to capture a particular area as I am not able to find the location and size of the application window.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
public class AppCheck {
public static void main(String[] args) {
System.out.println("***********************");
try {
System.out.println("Opening notepad");
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("notepad");
try {
int count=0;
while(process.isAlive())
{
System.out.println("process name : " + process.getClass().getName());
Field f = process.getClass().getDeclaredField("handle");
f.setAccessible(true);
long handl = f.getLong(process);
System.out.println("Process ID : " + handl);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Closing notepad");
process.destroy();
} catch (Exception ex) {
System.out.println(ex);
}
System.out.println("************************************");
}
}

Process doesn't stop in Java

I have following problem...I created a Process via ProcessBuilder in this way :
private ProcessBuilder processBuilder;
private Process process;
public void init() {
processBuilder = new ProcessBuilder(
"java", "-jar",
"bam.jar",
host,
);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
try {
process = processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
and I have function who should a kill process :
public void stop() {
process.destroy();
try {
process.waitFor(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (process.isAlive()) {
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
process.destroyForcibly();
}
}
But killing process sometimes work, but sometimes doesn't work. Any idea?
had similar problem, resolved it by replacing processBuilder.start() with
java.lang.Process process = java.lang.Runtime.getRuntime().exec("command java -jar some.jar");
proccess.destroy();

Run the ij tool (Derby) from java

I try to run the *.bat files of derby, but I can't do this.
I want to run the "startNetworkServer.bat" and "ij.bat" using by java code.
I try to write some simple code but it not working.
package dbconnect;
import java.io.IOException;
public class RunDerbyTools {
public static void main(String[] args) {
String pathIj, pathStartNetwork, pathStopNetwork;
pathIj = "C:/MyWorkSpace/MyDBProject/db/bin/ij.bat"; // running ij tool
// running network
pathStartNetwork = "C:/MyWorkSpace/MyDBProject/db/bin/startNetworkServer.bat";
// stop network
pathStopNetwork = "C:/MyWorkSpace/MyDBProject/db/bin/stopNetworkServer.bat";
try {
Process pStartNetwork = Runtime.getRuntime().exec(pathStartNetwork);
} catch (IOException e) {
e.printStackTrace();
}
try {
Process pPathIj = Runtime.getRuntime().exec(pathStartNetwork);
} catch (IOException e) {
e.printStackTrace();
}
}
}

How do I open wordpad using java

I am doing a small project for my girlfriends grandparents, that have a hard time using a computer so I thought I would be able to write something that might fix their problem. Here is the code first off:
import java.io.IOException;
public class OpenWordPad {
public static void main(String[] args) {
try {
System.out.println("Opening WordPad");
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("wordpad");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Closing WordPad");
process.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(had to indent some so sorry if it is a little wonky)
When I put notepad in the process line it works fine but when I put in wordpad it freaks out. I want to be able to open wordpad so I can put it on their computer. Any suggestions?
For that you can use runTime.exec("write"):
import java.io.IOException;
public class OpenWordPad {
public static void main(String[] args) {
try {
System.out.println("Opening WordPad");
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("write"); // <--- here
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Closing WordPad");
process.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
}
opens WordPad.

How to release process resource in java

How to release process resource??
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.StringTokenizer;
public class RuntimeSample{
public RuntimeSample() {
}
private void execCmd1() throws IOException {
InputStream in = null;
Process process = null;
String[] cmd = { "java", "-version" };
try {
process = Runtime.getRuntime().exec(cmd);
in = process.getInputStream();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
}
}
private void execCmd2() throws IOException {
Process process = null;
String[] cmd = { "java", "-version" };
try {
process = Runtime.getRuntime().exec(cmd);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
why it is throwing process.getError stream is not closed,I tried to close process resource by using following
if (process != null) {
process.getInputStream().close();
process.getOutputStream().close();
process.getErrorStream().close();
even it is showing process.getError stream is not closed.may i know the reason y it is showing that stream is not closed and how to close the process resource.Thanks in advance
I know this answer is somewhat late, but maybe someone else runs into the same issue.
In my project the Server runs some utility (I call it winps.exe here) regularly and it was easy to see (using RAMMAP resp. Handles) that the Java process kept a handle to each of the (terminated) child processes. Restarting the Server removed all entries from the process table.
After some experimenting, I found that explicitly calling the Garbage Collector resolved the issue.
Below you find my Java test program which I used to investigate the matter.
Note that this test program has been copied from the Server source code and isn't 100% as cleanly written as possible.
import java.util.*;
import java.io.*;
import java.util.regex.*;
import java.text.*;
import java.lang.reflect.*;
import java.util.concurrent.TimeUnit;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
public class StartPS
{
static Long STARTTIME_JITTER = 5000L;
synchronized public static HashMap<String,Long> getStartTimes()
{
HashMap<String,Long> result = new HashMap<String, Long>();;
final File tmp_file = new File("/tmp/starttimes.out");
String tmpfilename = null;
try {
tmpfilename = tmp_file.getCanonicalPath();
} catch (Exception e) {}
try {
ProcessBuilder pb = new ProcessBuilder(".\\winps.exe");
pb.redirectOutput(new File(tmpfilename));
pb.redirectErrorStream(true);
Process p = pb.start();
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();
try {
p.waitFor();
} catch (InterruptedException ie) {
// maybe some cleanup here
}
try {
while (p.isAlive()) { // <- most likely unnecessary ;-)
Thread.sleep(100);
p.destroy();
System.gc();
}
p = null; // should help the garbage collector (eliminates a reference to the object p points to)
System.gc(); // <- most likely the key to success !
} catch (Exception e) {
System.out.println("Warning: " + e.toString());
}
} catch (Exception e) {
throw new RuntimeException("(02310251044) Process start times : " + e.toString());
}
return result;
}
public static void main(String[] argv)
{
String sMax;
int iMax = 500;
if (argv.length > 0) {
sMax = argv[0];
try {
iMax = Integer.parseInt(sMax);
} catch (Exception e) {
System.out.println("Oops : " + e.toString());
System.exit(1);
}
}
for (int i = 0; i < iMax; ++i) {
System.out.print("\r" + i);
getStartTimes();
try {
Thread.sleep(1000);
} catch (Exception e) {
// do nothing
}
}
}
}

Categories