Executing Hive Query from Java - java

I tried to execute a small hive query from Java, but it is failing with below error, bur when I copy the same query and run on terminal it is giving me the result.
Can someone help me on this.
Java Code:
Runtime.getRuntime().exec("hive -e 'show databases;'");
Error thrown:
FAILED: ParseException line 1:5 cannot recognize input near '<EOF>' '<EOF>' '<EOF>' in ddl statement
Regards,
GHK.

I have been working with this Java problem for a while, and I believe I have solved this problem. Basically the reason you are failing is because the environment variables are not ser up properly. put the following in your /home/<username>/.bash_profile file and restart your machine to fix this.
HIVE_HOME=/usr/lib/hive
export HIVE_HOME
PATH=$PATH:$HIVE_HOME/bin/hive
export PATH
This will ensure that they get set up properly.
However while this will get rid of the error it still won't show you a list of databases because the process that runs the hive command will run in the background, not on the console the main program is running from. The following code will let you redirect the outputs of the program to the console that the main program is running from.
package testing.console;
import java.io.IOException;
import java.lang.ProcessBuilder;
import java.util.Map;
import testing.console.OutputRedirector;
//This Works
public class ConsoleTester {
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
ProcessBuilder hiveProcessBuilder = new ProcessBuilder("hive", "-e",
"show databases");
String path = processEnv.get("PATH");
Process hiveProcess = hiveProcessBuilder.start();
OutputRedirector outRedirect = new OutputRedirector(
hiveProcess.getInputStream(), "HIVE_OUTPUT");
OutputRedirector outToConsole = new OutputRedirector(
hiveProcess.getErrorStream(), "HIVE_LOG");
outRedirect.start();
outToConsole.start();
}
}
And the OutputRedirector class used to get the output to console.
package testing.console;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class OutputRedirector extends Thread {
InputStream is;
String type;
public OutputRedirector(InputStream is, String type){
this.is = is;
this.type = type;
}
#Override
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(type + "> " + line);
}
} catch (IOException ioE) {
}
}
}

Related

Error: Main class Test could not be found or loaded

I created an easy script with notepad++ and run in the cmd with this command:
java -cp . Test
Result:
Error: Main class Test could not be found or loaded
Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
String fileName = "npcData.csv";
String line = "";
String csvSplitBy = ";";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
while ((line = br.readLine()) != null) {
String[] npc = line.split(csvSplitBy);
System.out.println("nonCritSpecialNpc['" + npc[0] + "'] = true;");
}
} catch (IOException e) {
System.out.println("Fehler beim Lesen der Datei: " + e.getMessage());
}
}
}
What am I'm missing?
There are a couple of things you're missing here.
First, for public classes, the name of the class should match the name of the file. I.e., you either need to rename your class to Test, or rename the file to Main.java.
Second, you're missing a crucial step here - compiling. Java is not an interpreted language. You need to convert your .java class to something the JVM can run. This is done using the Java Compiler, or javac for short:
javac Main.java
After doing that, you'll have a .class file that the JVM can execute:
java -cp . Main

I can't run a python script from java and I think it's because the script does not have execute permissions

I'm trying to run a python script whenever a button on my gui (swing) is pressed. However, the script never runs and I'm not sure how to fix this. I know the script works fine independently, it should be py not python because windows, and my file system ntfs.
So far I've been trying to use code that can be summarized as below:
myBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Process p = Runtime.getRuntime().exec("py myScript.py");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
I don't think I can chmod ntfs stuff but I tried setting permissions via right clicking the python file and trying to mess with the security settings. Full control for the script to users does nothing.
The python script has the following permissions, my guess is my code isn't working because it does not have execute permissions.
-rw-r--r--
Use complete python executable path instead of "py". It executes the file with just read permissions.
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Sample {
public static void main(String[] args) throws Exception {
try {
Process p = Runtime.getRuntime().exec("C:/Windows/py myScript.py");
String cmdOutput = null;
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
// read the output from the command
while ((cmdOutput = stdInput.readLine()) != null) {
System.out.println(cmdOutput);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
myScript.py
print("This line will be printed.")
Output:
C:\Users\Administrator\Documents\demo>javac Sample.java
C:\Users\Administrator\Documents\demo>java Sample
This line will be printed.

Java, seemingly randomly, started crashing on FileHandle.class.getResourceAsStream(path);

So, I'm working on a program that allows you to import animations in the form of JSON files into Minecraft, and, when working on a completely different part of the program, my import code stopped working.
I'm using eclipse, and this is how my import code looks:
package com.github.sam54123.mc_animation.utils;
import java.io.InputStream;
public class FileHandle
{
public static InputStream inputStreamFromFile(String path)
{
try
{
InputStream inputStream = FileHandle.class.getResourceAsStream(path);
return inputStream;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
new file
package com.github.sam54123.mc_animation.utils;
import java.io.File;
import java.io.InputStream;
import java.util.Scanner;
import org.json.JSONObject;
public class JSONUtils
{
public static String getJSONStringFromFile(String path)
{
// Open file
Scanner scanner;
try
{
InputStream in = FileHandle.inputStreamFromFile(path);
scanner = new Scanner(in);
// Get JSON as string without spaces or newlines
String json = scanner.useDelimiter("\\Z").next();
// Close file
scanner.close();
return json;
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
return null;
}
}
public static JSONObject getJSONObjectFromFile(String path)
{
File file = new File(path);
if (!file.exists())
{
System.out.println("Invalid Path");
return null;
}
String string = getJSONStringFromFile(path);
return new JSONObject(string);
}
}
And I proceed to do some more fancy pampering of the file later on. This used to work reliably, until I made this in a completely different and un-related class:
String command = getCommand(object);
if (command != null && command.length() > 0)
{
commands.add(new AnimCommand(command, i));
}
And then it started throwing this error:
[Ljava.lang.StackTraceElement;#7852e922
Exception in thread "main" java.lang.NullPointerException
at java.io.StringReader.<init>(Unknown Source)
at org.json.JSONTokener.<init>(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:406)
at com.github.sam54123.mc_animation.utils.JSONUtils.getJSONObjectFromFile(JSONUtils.java:47)
at com.github.sam54123.mc_animation.system.Animation.<init>(Animation.java:20)
at com.github.sam54123.mc_animation.testing.Tester.main(Tester.java:13)
I've double checked that the file hasn't changed, and I tried deleting that section of code, restarting Eclipse, the whole deal, and nothing seems to fix it. The code is even able to recognize that the file is valid using the File class, but nothing seems to change. Does anyone have some insight on how this might be fixed? Here is the rest of my code: https://github.com/Sam54123/mc-animation/
EDIT
Okay, I've just done some more debugging, and it looks like it's the
return new JSONObject(string);
on line 47 of the second file that's crashing. No idea why, as the risky stuff of reading a file off disk is okay.
EDIT 2
It looks looks like it's failing because
InputStream in = FileHandle.inputStreamFromFile(path);
is returning null, which makes sense because of the try catch statement
InputStream inputStream = FileHandle.class.getResourceAsStream(path);
is in. Why that's failing beats me though, because the validity of the file is verified elsewhere in the code. It also used to work, and I haven't changed anything about the layout of the files.
EDIT 3
Interesting, a couple System.out.printlns reveal the catch is not actually getting activated, and therefore getResourceAsStream() must actually be returning null. I've confirmed this by printing it out before I return it.

How to Execute FreeSWITCH (fs_cli) from a java application

I am new to freeswitch, I have tried originate command in freeswitch from fs_cli console and it was working properly. now my requirement is to execute the same from a java application.
I have tried following code
package org.freeswitch.esl.client.outbound.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Call {
Call() throws IOException {
Process pr = Runtime.getRuntime().exec("./fs_cli -x \"originate loopback/1234/default &bridge(sofia/internal/1789#192.168.0.198)\"");
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String str = null;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
System.out.print("success");
}
public static void main(String[] args) throws IOException {
Call call;
call = new Call();
}
}
Output
-ERR "originate Command not found!
success
please help me,
fs_cli is at "/usr/local/freeswitch/bin/" location
I have created a symbolic link in my workspace directory.
why don't you use the ESL client? It should provide much more options, and originating a call would be no problem.
Regarding your particular problem, it looks like your program tried to execute "originate" command in the shell, not the ./fs_cli. Probably it needs more Java documentation reading :)

string arg[] causing java.lang.NoClassDefFoundError

So I have a script file we are using in house for testing. I want to use the script for testing over the internet but when I give it a url instead of a ip address it throws a java.lang.NoClassDefFoundError,
Why is this and what can I do to fix it
this is the script file:
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class SOAPClient4XG
{
public static void main(String[] paramArrayOfString)
throws Exception
{
if (paramArrayOfString.length < 2) {
System.err.println("Usage: java SOAPClient4XG http://soapURL soapEnvelopefile.xml [SOAPAction]");
System.err.println("SOAPAction is optional.");
System.exit(1);
}
String str1 = paramArrayOfString[0];
String str2 = paramArrayOfString[1];
String str3 = "";
if (paramArrayOfString.length > 2) {
str3 = paramArrayOfString[2];
}
URL localURL = new URL(str1);
URLConnection localURLConnection = localURL.openConnection();
HttpURLConnection localHttpURLConnection = (HttpURLConnection)localURLConnection;
FileInputStream localFileInputStream = new FileInputStream(str2);
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
copy(localFileInputStream, localByteArrayOutputStream);
localFileInputStream.close();
byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
localHttpURLConnection.setRequestProperty("Content-Length", String.valueOf(arrayOfByte.length));
localHttpURLConnection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
localHttpURLConnection.setRequestProperty("SOAPAction", str3);
localHttpURLConnection.setRequestMethod("POST");
localHttpURLConnection.setDoOutput(true);
localHttpURLConnection.setDoInput(true);
OutputStream localOutputStream = localHttpURLConnection.getOutputStream();
localOutputStream.write(arrayOfByte);
localOutputStream.close();
InputStreamReader localInputStreamReader = new InputStreamReader(localHttpURLConnection.getInputStream());
BufferedReader localBufferedReader = new BufferedReader(localInputStreamReader);
String str4;
while ((str4 = localBufferedReader.readLine()) != null) {
System.out.println(str4);
}
localBufferedReader.close();
}
public static void copy(InputStream paramInputStream, OutputStream paramOutputStream)
throws IOException
{
synchronized ()
{
synchronized (paramOutputStream)
{
byte[] arrayOfByte = new byte['Ā'];
for (;;) {
int i = paramInputStream.read(arrayOfByte);
if (i == -1) break;
paramOutputStream.write(arrayOfByte, 0, i);
}
}
}
}
}
and the bat file used to run it is this:
echo Check in inquiry sending:
java -cp .SOAPClient4XG http://foobar/gotdns/com:8080/axis2/services/HTNGListener checkininquiry.sms http://htng.org/1.1/Listener.Wsdl#ReceiveMessageAsync
here is the stack trace:
C:\Documents and Settings\accounting\Desktop\springer_miller_docs>java -cp .SOAP
Client4XG http://foobar.gotdns.com:8080/axis2/services/HTNGListener checkinin
quiry.sms http://htng.org/1.1/Listener.Wsdl#ReceiveMessageAsync
Exception in thread "main" java.lang.NoClassDefFoundError: http://foobar/gotd
ns/com:8080/axis2/services/HTNGListener
Your problem has nothing to do with your code but how you're executing it.
Your command line says put the main file onto the class path and then execute main in a class named the first URL.
Leave off -cp and you should be fine (at least from this stack trace).
For reference: http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/java.html
This line of code
URL localURL = new URL(str1);
will throw a java.net.MalformedURLException: no protocol: 192.168.0.127 for the argument you're giving it.
URL is protocol aware, and you should give it something like http://soapURL (as advised by your program itself) instead of a plain IP address.
edit
not relevant after you changed your question.

Categories

Resources