My bat file is:
#echo off
java -cp * MyTimerTasker
I try to run main function in MyTimerTaskerClass.
All jars and bat file are in same folder.
When I try to run bat file with double click, it runs.
When I try to run with right click and run as administrator, command window shows and disappears but my main function does not start.
When I try to run with task scheduler, it never starts.
Edit: My main class.
public class MyTimerTasker {
public static void main(String[] args) throws IOException {
FTPDownloadFiles ftpDownloadFiles = new FTPDownloadFiles();
System.out.println("Running ...");
DatabaseTask databaseTask = new DatabaseTask();
databaseTask.connectToDatabase();
ftpDownloadFiles.downloadFiles();
try {
databaseTask.parseFiles(JdbcConnection.filesPath);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
databaseTask.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
You are adding the classpath of all the files in the working directory. You should add the name of the jar to the classpath.
If you start as administrator, it starts in another directory (same if you start as a scheduled task).
You will have to set your working directory in the batchfile:
cd /d "%~dp0"
This will change the working directory to the folder, where your batchfile resides.
Related
I have a Java application, and when I use java.awt.Desktop:
Desktop.getDesktop().open(file);
It works fine on Windows (opens a file in my default program), but on Ubuntu (with openJdk 13), the Java application gets stuck and I do not even get any log error or anything. I have to force quit the app in order to recover.
The file path it correct, otherwise I would actually get an Exception. Also, isDesktopSupported a isSupported(Action.OPEN) returns true.
What can I do? Can I check some system settings or logs? Or perhaps get some logs from java.awt.Desktop? Or does this not work on Ubuntu/Linux?
Are there any alternatives?
From here:
In order to use the API, you have to call java.awt.EventQueue.invokeLater() and call methods of the Desktop class from a runnable passed to the invokeLater():
void fxEventHandler() {
EQ.invokeLater(() -> {
Desktop.open(...);
});
}
I am just going to add an example function
private static void OpenFile(String filePath){
try
{
//constructor of file class having file as argument
File file = new File(filePath);
if(!Desktop.isDesktopSupported())//check if Desktop is supported by Platform or not
{
System.out.println("not supported");
return;
}
Desktop desktop = Desktop.getDesktop();
if(file.exists()) { //checks file exists or not
EventQueue.invokeLater(() -> {
try {
desktop.open(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
I am trying to write something to a ini file using ini4j.
When i call the store() method it throws a FileNotFound exception even though it is in my project directory.
Maybe i did something wrong with my code?
Main:
public class Main {
public static Wini ini = null;
public static void main(String[] args) {
Config conf = new Config();
try {
conf.setMultiOption(true);
ini = new Wini();
ini.setConfig(conf);
ini.load(new File("apikeys.ini"));
} catch (InvalidFileFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
The file where i attempt to write and store the data:
if (KeyEndpoint.isValid(apikey)) {
Main.ini.put(apikey, Main.ini.get("Apikey"));
try {
Main.ini.store();
} catch (IOException e) {
channel.sendMessage("Invalid api key.").queue();
}
} else {
channel.sendMessage("API Key is invalid.").queue();
}
Any help is appreciated, I at least want to know what I am doing wrong.
Thanks!
It has to do with relative paths. Try changing the filename in Main for the absolute path, something like "/tmp/apikeys.ini", to check that your code works correctly. If that works, then you can now change it to either something like "../../directory/filename" or something relative to where you know you're executing your code from. Get familiar with your java path environment variables such as JAVA_HOME and all to get it to something more permanent and portable.
I am new to Java programming. Using eclipse for coding. I want to execute JarFile.jar. Following code works fine when placing JarFile.jar inside the same directory where the project is created.
public class CmdTest2
{
public static void main(String[] args)
{
final String dosCommand = "java -jar \"D:\\Java_codes\\CmdTest2\JarFile.jar\"
try
{
final Process process = Runtime.getRuntime().exec(dosCommand);
final InputStream in = process.getInputStream();
int ch;
while((ch = in.read()) != -1)
{
System.out.print((char)ch);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
But I want to execute the jar file from any other directory (say E:\Test\JarFile.jar). How can I do that? (i.e. changing directory through as well as triggering jar file).
In continuation to above pl. let me know how to run MS-DOS commands through Java.
I am writing a sample program using HCatalogue Streaming api
I have a running hadoop, Hiveserver and a Hivemetastore server.
I write a java program to connect to hive metastore.The source code is given below
public class HCatalogueStreamingclient {
public static void main(String[] args) {
System.setProperty("hadoop.home.dir", "E:\\midhun\\hadoop\\hive\\winutils");
String dbName = "hive_streaming";
String tblName = "alerts";
ArrayList<String> partitionVals = new ArrayList<String>(2);
partitionVals.add("Asia");
partitionVals.add("India");
HiveEndPoint hiveEP = new HiveEndPoint("thrift://192.168.10.149:8000", dbName, tblName, partitionVals);
HiveConf conf = new HiveConf();
conf.set("hive.exec.scratchdir", "/tmp/hivetmp");
try {
StreamingConnection connection = hiveEP.newConnection(true,conf);
} catch (ConnectionError e) {
e.printStackTrace();
} catch (InvalidPartition e) {
e.printStackTrace();
} catch (InvalidTable e) {
e.printStackTrace();
} catch (PartitionCreationFailed e) {
e.printStackTrace();
} catch (ImpersonationFailed e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
While running the program I am getting following exception
Exception in thread "main" java.lang.RuntimeException: The root scratch dir: /tmp/hivetmp on HDFS should be writable. Current permissions are: rw-rw-rw-
at org.apache.hadoop.hive.ql.session.SessionState.createRootHDFSDir(SessionState.java:690)
at org.apache.hadoop.hive.ql.session.SessionState.createSessionDirs(SessionState.java:622)
at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:550)
at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:513)
at org.apache.hive.hcatalog.streaming.HiveEndPoint$ConnectionImpl.createPartitionIfNotExists(HiveEndPoint.java:445)
at org.apache.hive.hcatalog.streaming.HiveEndPoint$ConnectionImpl.<init>(HiveEndPoint.java:314)
at org.apache.hive.hcatalog.streaming.HiveEndPoint$ConnectionImpl.<init>(HiveEndPoint.java:278)
at org.apache.hive.hcatalog.streaming.HiveEndPoint.newConnectionImpl(HiveEndPoint.java:215)
at org.apache.hive.hcatalog.streaming.HiveEndPoint.newConnection(HiveEndPoint.java:192)
at org.apache.hive.hcatalog.streaming.HiveEndPoint.newConnection(HiveEndPoint.java:113)
at com.mj.HCatalogueStreamingclient.main(HCatalogueStreamingclient.java:27)
Any one have an Idea on how to grand write permission to the hdfs folder /tmp/hivetmp
The problem is identified.
We need to grand all permission for hadoop directory /tmp/hivetmp.The command for granding permission is $HADOOP_HOME/bin/hadoop fs -chmod 777 -R /tmp/hivetmp
I was running the program in Windows 7 64 bit OS.We need to download winutils appropriate to the operating system and version.
After doing above given points I was able to establish connection using Hive HCatalogue Streaming api
I am creating a simple download program that opens a file with the web browser to download it. On Mac, there is a dialog when you open a downloaded executable JAR file that says you
can't open it because it is from an unidentified developer.
Is there a way to open the JAR file without the dialog by using Java code? Here is my code:
File newFile = new File(System.getProperty("user.home")+"/Library/AppTest/Application.jar");
File file = new File(System.getProperty("user.home")+"/Downloads/AppTest.jar");
try {
Files.move(file.toPath(), newFile.toPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The problem with the code is when it executes, it just shows this dialog, and will not let you open the file. I want to fix this without having to go to settings, because other users may have the same problem.
To open a JAR file on a mac, use Runtime:
String command = "java -jar path/to/jar/file";
Process p = Runtime.getRuntime().exec(command);
the above code is from this link.