Running a .bat inside Java Swing GUI Not Working - java

Using the following code, I can get the .bat file to execute (no GUI, just the following lines). However, when I add it in as a method of the ActionListener for the button (its a Java Swing app), the .bat file never executes. Any ideas?
Runtime runtime = Runtime.getRuntime();
try {
Process proc = runtime.exec("cmd /c start C:\\Users\\someName\\Desktop\\test.bat");
} catch (IOException e1) {
e1.printStackTrace();
}

Me too getting the same errors.
You can use like this
public void actionPerformed(java.awt.event.ActionEvent evt)
{
File file = new File("filename.bat");
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jButton1ActionPerformed(evt);
}

Related

DLL Method Not Working After Creating Exe Of Project

I have calling display method from dll file This works when I run my java project . But it stop working after creating of project exe file. My Code is as follow ..
static {
try {
Bridge.setVerbose(true);
try {
Bridge.init();
} catch (IOException e) {
e.printStackTrace();
}
File dll_File = new File("helloworld.j4n.dll");
Bridge.LoadAndRegisterAssemblyFrom(dll_File);
helloworld.Hello.display(str)
} catch (Exception exception) {
exception.printStackTrace();
}
}
Have you signed the dll? Please sign the dll and then check.

Using Desktop.browse doesn't allow program to exit automatically

If I make a simple program that browses to google.com, the program doesn't exit automatically when reaching the end of the main method. I would rather not call System.exit(0), so I was wondering if there was a way to "close" the desktop thread so it can exit automagically.
public static void main(String[] args) {
try {
Desktop.getDesktop().browse(new URI("http://google.com"));
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}

Running command with resource as argument

I am running a jar file on a different path on the system programmatically. One of the arguments needed is the path to a file, and the file is in my jar file. How can I pass the file (script.ospt) as an argument for another java application?
Current code:
Process ps = null;
try {
ps = Runtime.getRuntime().exec(new String[]{"java","-jar",engine.getAbsolutePath(), script3.getAbsolutePath()});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ps.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EDIT: More explanation:
I have a command I would like to run from java, using Runtime.getRuntime().exec().
The command I am running requires one argument: the path to a file. If the file is in the same package as my main class, how can I use the file in my program as an argument for the command?

How to open an exe file in java

I am trying to use java to open an exe file. I'm not sure which program I want to open so I am using Skype as an example. When I try to do it, it gives me errors.
try {
Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Skype\\Phone\\Skype");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
error:
Cannot run program "C:\Program": CreateProcess error=2, The system cannot find the file specified
Try this:
String path = "/path/to/my_app.exe";
File file = new File(path);
if (! file.exists()) {
throw new IllegalArgumentException("The file " + path + " does not exist");
}
Process p = Runtime.getRuntime().exec(file.getAbsolutePath());
You have to use a string array, change to
try {
Process p = Runtime.getRuntime().exec(new String[] {"C:\\Program Files (x86)\\Notepad++\\notepad++.exe"});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You are on windows so you have to include the extension .exe
try {
Process p = Runtime.getRuntime().exec("C:/Program Files (x86)/Skype/Phone/Skype.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Maybe use File.separator instead of '\'
I tried this and it works fine, it's taken from your example. Pay attention to the double \\
public static void main(String[] args) {
try {
Process p;
p = Runtime.getRuntime().exec("C:\\Program Files\\Java\\jdk1.8.0_05\\bin\\Jconsole.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Testing the launch of a module in a java ServerSocket?

I developed a Java ServerSocket that is always running and it executes many modules . My server start by execution a BatchScript file (start.bat). I want to test if the module is successfully executed and give the user w feedback on the cmd console(when i launch the server from eclipse i can see the long on eclipse console only) . I want to check my server statut after a periode of time knowing that it is always running .
Here is the implementation of my server :
public Server() throws InterruptedException
{
//this.es = e;
try {
sockserv= new ServerSocket(6020) ;
System.out.println("Server Started");
myModule mod= new myModule();
mod.displayLogOnCmd();
mod.checkServerEveryPeriodOfTime();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
#Override
public void run()
{
while(SERVER_STATUT)
{
try {
Socket socket = sockserv.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
My Question :
How to display log info of eclipse in the Cmd ? and how to see if the server launched the module or not ??
Thank You :)

Categories

Resources