Run external file Java [duplicate] - java

This question already has answers here:
Running Command Line in Java [duplicate]
(8 answers)
How to Execute Windows Commands Using Java - Change Network Settings
(5 answers)
What does "error: unreported exception <XXX>; must be caught or declared to be thrown" mean and how do I fix it?
(1 answer)
Closed 7 years ago.
How can I run an external file in Java?
Like opening a batch or a PDF file?
I want to add it to a jButton with Netbeans, so if somebody pushs on it, it will launch this file from a specified directory
Theres not a lot about this to find on the internet (except a web application, but it needs to be a file/batch/.exe ....
AS SAID its for Netbeans! Everytime I use those commands inside a Button Class:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("msg * hello");
}
I get an error for the line:
Process pr = rt.exec("msg * hello");
Which says:
unreported exception IOException; must be caught or declared to be
thrown
Now how do I launch an external file?

The Runtime class can be used to do this. You can get an instance of this class via the static method Runtime.getRuntime(). In the Runtime class are several exec() methods that should be what you need.
Finally, you may consider the ProcessBuilder class more convenient depending on how complex your needs are.

Related

Get full command line from Java [duplicate]

This question already has answers here:
Determining location of JVM executable during runtime
(5 answers)
Closed 10 months ago.
The community reviewed whether to reopen this question 27 days ago and left it closed:
Original close reason(s) were not resolved
How can I from within Java code find out which and how the running JVM was launched?
My code shall spawn another Java process hence I'd be especially interested in the first parameter - which usually (in C, Pascal, Bash, Python, ...) points to the currently running executable.
So when a Java application is run like
d:\openjdk\bin\java -Xmx500g -Dprop=name -jar my.jar param1 param2
I can access command line parameters in my main method like
public class Main {
public static void main(String[] args) {
System.out.println("main called with " + args);
}
}
but that will deliver access to param1 and param2 only. How would I get the full command line?
Following Determining location of JVM executable during runtime I found the real answer to be:
ProcessHandle.current().info().commandLine().orElseThrow();
It returns the full command line as perceived by the operating system and thus contains all information: executable, options, main class, arguments, ...
Thank you, #XtremeBaumer
To get the command, use ProcessHandle adapted from your answer:
String command = ProcessHandle
.current()
.info()
.command()
.orElse("<unable to determine command>"); // .orElseThrow() to get Exception
Use the Runtime Management Bean RuntimeMXBean to obtain the VM arguments and the class path, like in:
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
String classPath = mxBean.getClassPath();
List<String> inputArgs = mxBean.getInputArguments();
This will return all arguments passed to the virtual machine, not the parameters passed to the main method.
The code source can be get from the ProtectionDomain of the class.

JAVA program accepting Devanagari(UTF8) charaters [duplicate]

This question already has answers here:
Passing command line unicode argument to Java code
(7 answers)
Closed 5 years ago.
What settings are needed so that Java class main method can accept Marathi/Devanagarai/UTF-8 characters.
e.g.
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println("भारत");
}
In eclipse Java run arguments if I specify argument as "abc" it prints
abc
भारत
But If I specify Marathi/Devanagarai/UTF-8 string e.g. "कौशिक" then it prints
?????
भारत
What extra settings to be done in eclipse ?
Later I want to excute this program from jar using command and call it from PHP
e.g.
java -cp xyz.jar DevanagariTest कौशिक
What extra parameters will be needed at that time ?
I think it has something to do with your default system encoding. You can try to start the Java Programm with an additional Argument:
-Dfile.encoding=UTF-8

JAVA Console error [duplicate]

This question already has answers here:
System.console() returns null
(13 answers)
Closed 5 years ago.
If i run this code below in Netbeans
import java.io.Console;
public class Introduction {
public static void main(String[] args) {
Console console= System.console();// creates a java Object which has method that allows us to write
console.printf("Hallo parvel");
}
}
It gives me the error:
Exception in thread "main" java.lang.NullPointerException
at Introduction.main(Introduction.java:10)
/home/parvel/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds).
please help
java.io.Console object is null because there is no console attached to it.
Try to run from command line or attached console to it.
Find more details about it here: https://netbeans.org/bugzilla/show_bug.cgi?id=111337
You need to run this from a terminal using java -jar myjar.jar
In order to have this print to console using System.console();
You will need to export this as a runnable jar
then you can run java -jar myjar.jar and it will output Hallo parvel

Finding Main Class in "Hello World" program in Java [duplicate]

This question already has answers here:
How to execute a java .class from the command line
(7 answers)
Closed 6 years ago.
I have a seemingly simple program in Java, but when I run it, I get the error:
Error: Could not find or load main class
Here is my code.
public class HelloPrinter
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
I'm entirely new to the Java language, and I don't know what else to do. My program file is named "hello_world.java", and when attempting to run the program, I type in "java hello_world.java". Am I doing something fundamentally wrong? I've also attempted "java -cp hello_world", but that gave me the same error.
What am I doing wrong?
From what little information you've given, I'd say you're executing it the wrong way.
Make sure you run the following two commands:
javac HelloPrinter.java
java HelloPrinter
The first command compiles your source code into a .class file. The second command executes that class file.

Java Compile Error (HelloWorldApp); It shows "Error while writing" and "Access Denied" [duplicate]

This question already has answers here:
Java Access Denied
(3 answers)
Closed 8 years ago.
I use Windows 7 and recently installed JDK (jdk1.8.0_05). (I've also installed JRE)
As I'm new to Java, I complied the Hello World Java code given on Official Java Website:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
I saved the file as C:\HelloWorldApp.java
And then in command prompt, I typed
javac HelloWorldApp.java
I got the following message:
C:/HelloWorldApp.java:5: error: error while starting HelloWorldApp: C:\HelloWorldApp.class (Access is denied)
class HelloWorldApp{
^
1 error
PS: I even tried making the class public. Also, I've specified PATH variable as C:\Program Files\Java\jdk1.8.0_05\bin
What may be the solution?
Add public keyword before the class keyword. That is, change:
class HelloWorldApp {
to
public class HelloWorldApp {
The class that contains main method must be declared as public.

Categories