JAVA program accepting Devanagari(UTF8) charaters [duplicate] - java

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

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 - how can i type in user input using cmd command [duplicate]

This question already has answers here:
How to get input via command line in Java? [closed]
(4 answers)
Closed 1 year ago.
this may be blurry to grasp but i'm new to java whole thing and i want to know how can i use cmd to type something in user input
E:\My apps\Java apps\test\src\main\java>java ls.java && echo E:/
enter File name or path :
in this input i want to enter the path via cmd the "&& echo E:/" doesn't work
You can use "Command Line Arguments" in order to give the input when running the program!
Assuming your file name is ls.java
compile it using: javac ls.java
when running it using java command type the arguments in front of it like:
java ls E:/path
Now you can catch the arguments in the program
public class ls {
public static void main(String[] args)
{
String path = args[0];
}
}
you can add more arguments by spaces and catch them in String array
java ls E:/path 100 200
System.out.println(args[1]); // will print 1
System.out.println(args[2]; //will print 200
NOTE: As implied all the command line arguments are stored in the form of String, if you want to convert them to another format, you'd need to parse them using methods.

Beginner: Can't run this program eventhough it is the solution [duplicate]

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

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.

Categories