I am trying to get my java console application to be able to take commands both from Windows PS and Linux Terminal while being run. It isn't working properly though because of a special character (like äüö etc) in a command.
When the program is being executed in Linux everything works fine because of UTF-8. My simple program just takes an input via a scanner and checks whether or not it matches a specific String. My expected command is read from a .txt file and is in UTF-8 format.
I tried to
Arrays.equals(userInputAsString.getBytes(StandardCharsets.UTF_8), expectedInputToExecuteCommandAsString.getBytes(StandardCharsets.UTF_8))
But unfortunately it doesn't work while being run in PS.
I hope someone may be willing to help me!
Related
When I execute Java program as a user in terminal with command:
java -jar progName.jar
I receive stable and workable program which writes info to some files. But when I wrote some commands for cron scheduler to execute this program regularly, program works fine, but text in files is written in wrong encoding and I receive not the text but ???.
I use Ubuntu 14.04 on my server machine.
Thanks for your answers and tips. I solve this problem using a comment of user eg04lt3r:
This issue is depends on OS default encoding. If you didn't define direct encoding when write to file, it uses default OS encoding. So, add encoding for characters when you write data in file in your app.
So, I added encode way to my writer in Java program, and now everything works fine.
In my application, I'm reading the properties file(UTF-8 encoded) containing Chinese characters and printing them on the Windows command line. But,for some reason the the messages are not getting displayed correctly(some extra characters are coming). However the same mesages are getting displayed correctly on Eclipse console and Cygwin. I've set the command line code page to utf-8(65001) and used "Lucida" font as well.
If you see the above image, on Windows it printed one extra 0 on the second line, but that is not expected;on cygwin the message was printed correctly.
Please let me know if I'm missing something. From this post , I can see that there are some issues with Windows UTF-8 code page implementation. If so, is there any other way to get over this problem ?
I can see that there are some issues with Windows UTF-8 code page implementation
oh most surely yes
is there any other way to get over this problem ?
The typical solution is to accept that the Windows command prompt is a broken disaster and move on. But if you really have to, you can use JNA to call the Win32 function WriteConsole directly, avoiding the broken byte encoding layer, when you can determine you are outputting to the console (rather than eg a pipe).
I have an issue where I think my local JVM is running with a different character encoding to one that runs on a server. Is there a command I can pass to the vm to display what character encoding is has? Something like
java show 'charset'
I've been searching for ages, and all of the solutions require writing a test in java code, but I don't really want to have to write code, deploy it to the server, etc.
I have written an application which takes folder path and perform some operation. But if folder path contains characters of language other than English then it is not working. It only support English characters. How to handle this problem?
Theoretically it should work. Practically in most cases it is a problem of passing parameters from shell to java application. Try to verify that this works correctly: run application from command prompt exactly as you are doing but with remote debugging. Create breakpoint in your code where you are reading the parameter and see what is the value. I believe that the value is corrupted at this phase. This is a typical problem if you are on windows.
In this case you have to configure your system to support unicode characters in command prompt correctly.
If the path is read correctly from command line try to continue debugging and see where it is broken. Check which separator are you using. Avoid using both / and \, use File.separator instead.
I have a console based (System.in and System.out based) Java standalone application. I am trying to convert it into an applet. But the problem is that there is no console in Java applet! In other words, when I write to system output it is not displayed to the user and similarly I am unable to get input from user in absence of a console.
I know that a console lookalike can be done using TextArea and KeyListener, but for that I would need two different components, one for input and one for output. I want the input and output to go into the same component. Any ideas?
But there is a standard console for Java applets :)
http://www.java.com/en/download/help/javaconsole.xml
http://download.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/console.html
Joking aside, there does seem to be some console code on this site: http://math.hws.edu/eck/cs124/javanotes2/source/index.html. Apparently you can just extend this class and it'll give you a console in your applet. However, you may need to redirect System.out (standard output), System.err (error output) and System.in (input) into the console. That doesn't seem to be implemented but could be blocked in applets by the SecurityManager.
If you make your JTextArea editable, you can then listen for DocumentEvents that indicate when a user has entered text.
That said, and not knowing anything about your applet, it's generally nicer from a UI perspective if the user does not have to type into a console. Consider breaking the input into distinct fields, with a clear transition from input to execution to output.
The way I always run a console based Java program without downloading anything is running over to the trust command prompt terminal.
Click windows button + r (or open file explorer or program search)
Next, type "cmd" into the search box and hit enter.
Now, in the command prompt terminal, type java -jar xx.jar without quotes and replacing xx with the program name.
That's it! It's as simple as that! If you would like to make a file that you click on that automatically runs your Java application, continue with these steps:
Make a new text document where you want your start program file to be.
In the text document, type:
#echo off
start java -jar xx.jar
exit
Save the file as a .bat file.
That is how you make a batch file to run your Java application!