Selenium Chrome Driver In Tomcat is not working why? - java

was trying run my selenium automation code using java in a Tomcat server. It works fine when I run using javac but when it gets run on Tomcat as a jar It shows "com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V|" this as a log. Here my selenium-chrome driver is placed in desktop of my local machine and path is defined (Tomcat is also a local server)

I would go with a buffered file reader like this:
public static void main(String[] args) throws IOException {
try {
File f = new File("data.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine;
while ((readLine = b.readLine()) != null) {
if (readLine.contains("WORD"))
System.out.println("Found WORD in: " + readLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
where "WORD" is the word you are searching for.
The advantage of a BufferedReader is that it reads ahead to reduce the number of I/O roundtrips - or as they put it in the JavaDoc: "Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines."
FileChannel is a slightly newer invention, arriving in the NIO with Java 1.4. It might perform better than the BufferedReader - but I also find it a lot more low-level in its API, so unless you have very special performance requirements, I would leave the readahead/buffering to BufferedReader and FileReader.
You can also say that BufferedReader is "line oriented" whereas FileChannel is "byte oriented".

I like the BufferedReader from Java.io with a FileReader most:
https://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html
https://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/
It is easy to use and has most functions. But your file mus be char-based to use that ( like a text file)

Related

Vscode doesn't recognize Umlaute (äöü) when reading and writing files with Java

I have a Java project which reads from a .txt file and counts the frequency of every word and saves every word along with its frequency in a .stat file. The way I do this is by reading the file with a BufferedReader, using replaceAll to replace all special characters with spaces and then iterating through the words and finally writing into a .stat with a PrintWriter.
This program works fine if I run it in Eclipse.
However, if I run it in VSCode, the Umlaute (äöü) get recognized as Special characters and are removed from the words.
If I don't use a replaceAll and leave all the special characters in the text, they will get recognized and displayed normally in the .stat.
If I use replaceAll("[^\\p{IsAlphabetic}+]"), the Umlaute will get replaced by all kinds of weird Unicode characters (for Example Ăbermut instead of Übermut).
If I use replaceAll("[^a-zA-ZäöüÄÖÜß]"), the Umlaute will just get replaced by spaces. The same happens if I mention the Umlaute via their Unicode.
This has to be a problem with the encoding in VSCode or perhaps Powershell, as it works fine in other IDEs.
I already checked if Eclipse and VSCode use the same Jdk version, which they did. It's 17.0.5 and the only one installed on my machine.
I also tried out all the different encoding settings in VSCode and I recreated the project from scratch after changing the settings, to no avail.
Here's the code of the minimal reproducable problem:
import java.io.*;
public class App {
static String s;
public static void main(String[] args) {
Reader reader = new Reader();
reader.readFile();
}
}
public class Reader {
public void readFile() {
String s = null;
File file = new File("./src/textfile.txt");
try (FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);) {
s = bufferedReader.readLine();
} catch (FileNotFoundException ex) {
// TODO: handle exception
} catch (IOException ex) {
System.out.println("IOException");
}
System.out.println(s);
System.out.println(s.replaceAll("[a-zA-ZäöüÄÖÜß]", " "));
}
}
My textfile.txt contains the line "abcABCäöüÄÖÜß".
The above program outputs
abcABCäöü����
 äöü����
Which shows that the problem is presumably in the Reader, as the glibberish Unicode symbols don't get picked up by the replaceAll.
I solved it by explicitly turning all java files and all .txt files into UTF-8 encoding (in the bottom bar in VSCode), setting UTF-8 as the standard encoding in the VSCode settings and modifying both the FileReader and FileWriter to work with the UTF-8 encoding like this:
FileReader fileReader = new FileReader(file, Charset.forName("UTF-8"));
FileWriter fileWriter = new FileWriter(file, Charset.forName("UTF-8"));

Batch reading error (Train Schedulling) on Java NetBeans

I am currently enrolled in a Real Time class and I need to program an adaption to a workload-scheduler module of hybrid big data (stream) system.
I am starting from scratch: using Java on NetBeans, generating synthetic dataset, reading this data set with a Thread.
So what's going on: My thread is supposed to take different file readings and, depending on the size of the file, send it to a different ArrayList on my execution. It works fine for small files such as <10KB, but I also need to I/O batch files with size 64MB+ and medium files with variable size between 1KB and 64MB. Each one of these files will be treated accordingly.
Problem: My thread(single) reads all small .txt, but when reading larger files, my NetBeans just keep running and don't really do anything.
Could it be memory problems?
Does anyone have any knowledge of how to manipulate large txt files in NetBeans?
P.S.: Memory is not an issue on my computer.
In java you can read entire file into memory with:
java.nio.file.Files.readAlLines
Or you can use streaming with java.util.Scanner which reads file line by line or by token that matches pattern:
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream(path);
sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
String line = sc.nextLine();
// System.out.println(line);
}
// note that Scanner suppresses exceptions
if (sc.ioException() != null) {
throw sc.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
So to read large txt files use Scanner.

Reading input/output from Chatting program[exe file]

what i tried
try {
File fileDir = new File("B:\\Palringo\\palringo.exe");
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream("B:\\Palringo\\palringo.exe"), "UTF8"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
Output :
unreadable Strings
what i want
i want to control the (palringo.exe) so i can make Bot for it
What is palringo.exe ?:
its a chatting program you can download it or use web version (palringo.im).
am i doing wrong by opening a file that is exe ? should i connect to the website by Connection classes in java ? if so , how i can connect it ?
This doesn't work. You cannot read an exe file.
You need to have the source code or library to add that software to you code. You simply cannot read a exe file and extract code, because exe file will be encrypted and it will be in lower level languages.
But you can use exec() to run that exe file.
I know this is a very late answer, if you are looking to connect and manipulate palringo, there are a couple of APIs available.
https://github.com/calico-crusade/PalringoApi
This specific one can also be found on Nuget, though it is for C#. You could copy over the majority of the connection code to Java if you wish.

When using java runtim.exec to ssh, how do I get console output of that remote machine?

Say we have this method to make an ssh to another machine. How would I get the output from that machines terminals back to the host machine
public void getSSHreply()
{
Process p;
// Set up the arguments for ProcessBuilder
String[] cmd =
{
"/usr/bin/ssh",
"someRemoteMachine", //This machine will authenticate with keys, hence no pw needed
"./myprog",
};
try
{
p = Runtime.getRuntime().exec(cmd);
//How would I redirect stdout back to host machine?
StringBuffer s = new StringBuffer();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (input.readLine() != null)
{
s.append(input.readLine() + "\n");
}
System.out.println(s.toString());
}
catch (IOException e)
{
System.err.println("Failed to read & or start ");
}
}
The Process object has methods for getting the STDOUT, STDERR, and STDIN streams. (ex. getOutputStream()).
You may want to look into commons-exec for more convenient ways to launch and manager external programs, which has tools like StreamPumper to redirect data.
Unfortunately you can't. The simplest way is probably to read p's inputStream and errorStream (normally in two separate threads).
I believe your immediate problem is because you are using a BufferedReader - so when SSH displays the "Password: " prompt (which doesn't have a terminating line feed) Bufferedreader won't be returning anything to your input.readLine() call.
The easiest thing is to read the input a single character at a time (though not the most efficient, of course).
You'll also probably want to read the stderr stream as well, which is why you might want a couple of threads.

determine version of microsoft office with java

I wrote a program that creates a set of data that is outputted to an excel spreadsheet. I was originally using the jexcel library to write the data to the file, but I'd like to update the program so that it can check and see whether is should create a ".xls" or ".xlsx" file, then write to the appropriate document type. Apache POI seems to be the best option in terms of writing to a ".xlsx" file, but any ideas about determining the correct file type?
I could just have the user choose when naming the file, but that seems like extra work for the user and I'm assuming that there are users who don't know what file type they'd want.
Any ideas?
Also, I'm assuming the OS is windows and the user has some version of excel, in other cases I'll just choose a default file type.
One way is to call the Windows ASSOC and FTYPE commands, capture the output and parse it to determine the Office version installed.
C:\Users\me>assoc .xls
.xls=Excel.Sheet.8
C:\Users\me>ftype Excel.sheet.8
Excel.sheet.8="C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE" /e
Here a quick example :
import java.io.*;
public class ShowOfficeInstalled {
public static void main(String argv[]) {
try {
Process p = Runtime.getRuntime().exec
(new String [] { "cmd.exe", "/c", "assoc", ".xls"});
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String extensionType = input.readLine();
input.close();
// extract type
if (extensionType == null) {
System.out.println("no office installed ?");
System.exit(1);
}
String fileType[] = extensionType.split("=");
p = Runtime.getRuntime().exec
(new String [] { "cmd.exe", "/c", "ftype", fileType[1]});
input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String fileAssociation = input.readLine();
// extract path
String officePath = fileAssociation.split("=")[1];
System.out.println(officePath);
}
catch (Exception err) {
err.printStackTrace();
}
}
}
You may want to add more error checking and the parsing to extract the Office version from the returned path is left as an exercise ;-)
You can search in the registry for the key:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths
This will probably require some work, as evidenced by this question:
read/write to Windows Registry using Java
If you're willing to dive into the registry (eg with jregistrykey) a translated version of this PowerShell script should do what you want.
Take a look at OfficeVer.
You can implement it to your script or use it for code analysis. It's cross-platform much like Java, so compiling it and implementing it directly shouldn't be a big deal.
It works by extracting .docx and xlsx files and then reading the version, as well as reading directly from .doc and .xls files.
OfficeVer as well has extended their support to .pdf files (current version as of time of writing is 1.03.1)

Categories

Resources