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
Related
This question already has answers here:
System.console() returns null
(13 answers)
Closed 3 months ago.
I am trying to get the input from user by using System.console. However, I get a NullPointerException when I run the following code (I am trying to get input from the user).
import java.io.*;
public class Systemlearn {
public static void main(String[] args)throws IOException {
Console c=System.console();
System.out.println("Enter the Name:");
String str=c.readLine();
System.out.println(str);
}
}
Output:
Enter the Name:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.Console.readLine()" because "c" is null
at week5.Systemlearn.main(Systemlearn.java:7)
Are you running this from an IDE? If yes then that is the problem since System.console() is not available in an IDE environment.
Try running it from the console (Command Prompt / Terminal) and it should work fine.
Edit: Just tried in my console and it worked (see screenshot below)
This question already has answers here:
Java ArrayIndexOutOfBound
(4 answers)
Closed 4 years ago.
public class Exercise10 {
public static void main(String[] args) {
System.out.println("args[0] = " + args[0]);
System.out.println("args[1] = " + args[1]);
System.out.println("args[2] = " + args[2]);
}
}
The error message I get is: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
The "args" String array argument to main() is populated by the command-line arguments you give to your program when you run it. Since you presumably haven't given it any, looking up the 0th one is quite reasonably an ArrayIndexOutOfBoundsException.
If you are invoking this with java on the command line, try $ java Exercise10 arg0 arg1 arg2
If you are invoking it from an IDE, you'll need to figure out how that IDE supports passing command line arguments. For example, in Eclipse, I believe there should be an Arguments tab in the Run dialog.
"Arg" as said in the names equals to Arguments, You haven't passed any Argument to your program hence it raises the error ArrayIndexOutOfBound, You can pass Arguments to your program by executing it as a jar in command prompt java -jar myJar.jar a b c
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
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.
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.