The title says it all but here are a few more details. I've exported my java program as a runnable jar via eclipse. During the runtime it logs data which should be saved in a text file and in addition to that some infos are printet to the console just for info purposes.
When i start the jar via double-klick on it, it creates the file when it does not exists and does its job propperly but obviously no output to a console because there is none.
To get my console outputs i've tried to launch the jar via the windows console with this command: java -jar c:\path-to-the-jar\myJar.jar. It works and prints my outputs to the console but now it does not create the file nor write to an existing one. I've tried to start the console as admin but that was not the solution. I also tried this command: javaw -jar c:\path-to-the-jar\myJar.jar but then it does not output to the console BUT it writes to the file...
Do i have to use special startconditions or other commands? I have really no clue where to search and what to google for.
Thanks in advance! -Phil
Here's my code which handles the output and the file writing
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.nio.charset.StandardCharsets;
class DataLog {
private Path path;
private LocalDate actualDate;
private long errorCount = 0;
DataLog() {
try
{
path = Paths.get("LSV_DataLog_" + LocalDate.now() + ".txt"); //e.g. LSV_DataLog_2019-10-17.txt when its october 17th 2019
actualDate = LocalDate.now();
String str = "***" + LocalDateTime.now().toLocalDate().toString() + " - " + LocalDateTime.now().toLocalTime() + "***";
System.out.println(str);
Files.write(path, (str + System.lineSeparator()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
errorCount++;
} catch (Throwable t) {
t.printStackTrace();
errorCount++;
}
}
void addData(long val) {
try
{
if(!actualDate.equals(LocalDate.now())) {
actualDate = LocalDate.now();
path = Paths.get("LSV_DataLog_" + LocalDate.now() + ".txt"); //e.g. LSV_DataLog_2019-10-17.txt when its october 17th 2019
}
String str = LocalDate.now() + ", " + LocalDateTime.now().toLocalTime() + " Value: " + val;
System.out.println(str);
Files.write(path, (str + System.lineSeparator()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
errorCount++;
} catch (Throwable t) {
t.printStackTrace();
errorCount++;
}
}
void addInfo(String info) {
try
{
if(!actualDate.equals(LocalDate.now())) {
actualDate = LocalDate.now();
path = Paths.get("LSV_DataLog_" + LocalDate.now() + ".txt"); //e.g. LSV_DataLog_2019-10-17.txt when its october 17th 2019
}
String str = LocalDate.now() + ", " + LocalDateTime.now().toLocalTime() + ", " + info;
System.out.println(str);
Files.write(path, (str + System.lineSeparator()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
errorCount++;
} catch (Throwable t) {
t.printStackTrace();
errorCount++;
}
}
}
javawcommand by switching to a non-system directory?