I am trying to execute pig scripts from java so that it can connect to the cluster and execute the logic. I am following this link, but how this is going to connect my cluster as i am not mentioning the URL. How can i connect it remotely ?
Code:
import java.io.IOException;
import org.apache.pig.PigServer;
public class idlocal{
public static void main(String[] args) {
try {
PigServer pigServer = new PigServer("local");
runIdQuery(pigServer, "passwd");
}
catch(Exception e) {
}
}
public static void runIdQuery(PigServer pigServer, String inputFile) throws IOException {
pigServer.registerQuery("A = load '" + inputFile + "' using PigStorage(':');");
pigServer.registerQuery("B = foreach A generate $0 as id;");
pigServer.store("B", "id.out");
}
}
Related
In my idea IDE, I can see the compile error with red font in the console.But when I deploy the jar in the linux server.I can not see the compile log.How to print the compile error log?
public static void main(String[] args) throws Exception {
String compliePath="D:\\testFole";
String filename="D:\\test.java";
String[] arg = new String[] { "-d", compliePath, filename };
System.out.println(com.sun.tools.javac.Main.compile(arg));
}
Well if I got your question right, here is an approach to the outcome.
I think this will be platform-independent.
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
private static Process process;
public static void main(String[] args) {
runCommand();
getErrorMessage();
}
/**
* This method executes/runs the commands
*/
private static void runCommand()
{
File file = new File("D:\\\\test.java");
String changeDirectory = "cmd start cmd.exe /c cd D:\\";
String compile = " && javac D:\\test.java";
String run = " && java "+file.getName().replace(".java","");
String command = changeDirectory + compile + run;
try {
process = Runtime.getRuntime().exec(command);
}catch (IOException e){}
}
/**
* This method will get the errorStream from process
* and output it on the console.
*/
private static void getErrorMessage()
{
try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())))
{
String line;
if(errorReader.readLine() != null)
while ((line = errorReader.readLine()) != null)
System.out.println(line); //display error message
}catch (IOException e){}
}
}
For example, I've test.pptx open in Microsoft PowerPoint 2013 on Windows 10. I want to close it without closing Microsoft PowerPoint itself. How can I do that using Java 1.8?
Because it's external program, you need to kill your PC running power point task.
You can achieve it by using below Java code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ClosePowerPoint {
private static final String TASKLIST = "tasklist";
private static final String KILL = "taskkill /IM ";
public static void main(String args[]) throws Exception {
System.out.print(isProcessRunging("POWERPNT.EXE"));
if (isProcessRunging(processName)) {
killProcess(processName);
}
}
public static boolean isProcessRunging(String serviceName) throws Exception {
Process p = Runtime.getRuntime().exec(TASKLIST);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
if (line.contains(serviceName)) {
return true;
}
}
return false;
}
public static void killProcess(String serviceName) throws Exception {
Runtime.getRuntime().exec(KILL + serviceName);
}
}
I am working on a simple java program which runs another java program which is located at any location in the file system. Here is the code:-
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
class CompileTest {
public static void main(String args[]) {
try {
int k = runProcess("javac H://Study//eclipse_workspace//advance//src//Hello.java");
if (k==0)
k=runProcess("java H://Study//eclipse_workspace//advance//src//Hello");
} catch (Exception e) {
e.printStackTrace();
}
}
private static int runProcess(String command) throws Exception {
Process pro = Runtime.getRuntime().exec(command);
printLines(command + " stdout:", pro.getInputStream());
printLines(command + " stderr:", pro.getErrorStream());
pro.waitFor();
//System.out.println(command + " exitValue() " + pro.exitValue());
return pro.exitValue();
}
private static void printLines(String name, InputStream ins) throws Exception {
String line = null;
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
while ((line = in.readLine()) != null) {
System.out.println(name + " " + line);
}
}
}
Here is Hello.java
class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello");
}
}
But when it is compile it give output as:-
java H://Study//eclipse_workspace//advance//src//Hello stderr: Error: Could not find or load main class H:..Study..eclipse_workspace..advance..src..Hello
I am using Eclipse IDE
Try something like: java -classpath . Hello.
If you didn't specified package you must run Hello.class from the same directory.
runProcess(cd H:/Study/eclipse_workspace/advance/src/)
And then
runProcess(java Hello)
I can't run my ssh-keygen.exe. Output said build successful but the code should execute the .exe and display the application. This is my code
import java.io.IOException;
public class SSHConnectPing {
public static void main(String... args) throws IOException {
try
{
Runtime.getRuntime().exec("C:\\ExecuteSSH\\ssh-keygen.exe");
}
catch(Exception exc)
{
System.out.println("error" + exc);/*handle exception*/
}
}
}
What should I do? Please help me.
Thanks Jason now i can execute my .exe application
My code now is
package apacherunsshkeygen;
import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
public class ApacheRunSSHKEygen {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws IOException {
try {
// String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
String line = "C:\\ExecuteSSH\\ssh-keygen.exe";
CommandLine cmdLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
//watchdog
executor.setExitValue(1);
ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
executor.setWatchdog(watchdog);
int exitValue = executor.execute(cmdLine);
}
catch (Exception exc){
System.out.println("error" + exc);/*handle exception*/}
}
}
Java newcomer like me who are looking on how to execute external application (.exe) you can try this sample:
// get apache.common.exec.jar at:
http://commons.apache.org/proper/commons-exec/download_exec.cgi
import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
public class RunRsync {
public static void main(String[] args) throws IOException {
try {
//example : String line = "C://file.exe";
String line = "cmd /c start"; //you can put your .exe path here, like mine i run my window cmd
CommandLine cmdLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);
}
catch (Exception exc){
System.out.println("error" + exc);/*handle exception*/}
}
}
I am a PHP/Mysql programmer trying to learn Java and I'm stuck on how to complile a file with this in it:
public static void main(String[] args)
Here is example code:
import java.net.*;
import java.io.*;
public class GreetingClient
{
public static void main(String [] args)
{
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try
{
System.out.println("Connecting to " + serverName
+ " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out =
new DataOutputStream(outToServer);
out.writeUTF("Hello from "
+ client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in =
new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
When run the try to use the javac command in Windows Command prompt to compile this from a .java file into a .class file to be called in a webpage, I get an error saying:
bad class file: .\String.java
file does not contain class String
Please remove or make sure it appears in the correct subdirectory of the classpath.
public static void main(String[] args) {
If a compile a .java file like this one:
import java.applet.Applet;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
public class applet_test extends Applet {
private InetAddress addr = null;
public void init() {
try {
addr = InetAddress.getLocalHost();
}
catch (UnknownHostException e) {
System.exit(0);
}
}
public InetAddress getLHost() {
return addr;
}
}
I get no errors, the .java file compiles into a .class file and I am able to use the .class file in a webpage just fine.
Not sure what I'm doing wrong.
Thanks guys!
Ok, so now, when I run this command:
C:\ > javac GreetingClient.java
I get this:
GreetingClient.java:9: cannot find symbol
symbol : method parseInt(GreetingClient)
location: class java.lang.Integer
int port = Integer.parseInt(args[1]);
^
GreetingClient.java:14: cannot find symbol
symbol : constructor Socket(GreetingClient,int)
location: class java.net.Socket
Socket client = new Socket(serverName, port);
^
2 errors
Again, I only seem to get errors when running the javac command on a file with this line in it:
public static void main(String[] args)
I know I'm missing something, any help would be appreciated it.
Since your file contains class GreetingClient, it must be named GreetingClient.java. Java requires that the name of the file match the name of the class defined inside it.
In Java, your main class must contains public static void main(String[] args) to initialize it.
If you have more than one class, the other will be just class Class_Name {} and, as Greg Hewgill said: your Java File must be named equal your main class name