Use JGit to access existing clone? - java

I have a clone of a repository on my computer already. I wish to create a Java program that pulls when it opens and pushes when it closes.
This is how I initiate JGit:
auth = new UsernamePasswordCredentialsProvider("[username]", "[pass]");
git = Git.open(new File(path_to_git + "\\.git"));
git.checkout().setName("master").call();
git.pull().setCredentialsProvider(auth).call();
This works the first time I start up, but if I close and reopen the program I get the error
Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$2(LauncherImpl.java:352)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsupportedOperationException: Cannot check out from unborn branch
at org.eclipse.jgit.api.CheckoutCommand.call(CheckoutCommand.java:235)
at com.company.app.Main.<clinit>(Main.java:59)
... 11 more
When the program closes I call the function pushToGit() which is defined as:
public static void pushToGit() {
try {
git.remoteAdd()
.setName("origin")
.setUri(new URIish("https://github.com/[username]/[repo]"))
.call();
git.commit().setMessage("from database application").setAuthor(new PersonIdent("[name]","[email]")).call();
git.push().setCredentialsProvider(auth).call();
} catch (GitAPIException | URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Whenever I run the program, I need to delete the local repo and reclone it.

You can try with the following example code snippet.
Git git = Git.cloneRepository()
.setURI( "https://github.com/someRepoName" )
.setDirectory( "/path/to/repo" )
.setBranch( "refs/heads/master" )
.call();
You can refer below the link for more details.
https://www.codeaffine.com/2015/11/30/jgit-clone-repository/
In the higher version of Jgit api, you can use CloneCommand, you can check below the link.
https://www.programcreek.com/java-api-examples/?api=org.eclipse.jgit.api.CloneCommand

Related

HDFS FileSystem close Exception

I just ran a hdfs demo like this:
public final class HDFSRemoveDemo {
public static void main(String[] args) throws Exception {
Path root = new Path("hdfs://localhost:49000/");
FileSystem fs = root.getFileSystem(new Configuration());
fs.create(new Path("/tmp/test"));
fs.delete(new Path("/tmp/test"), false);
fs.close();
}
}
A puzzling exception threw as follows:
org.apache.hadoop.hdfs.DFSClient closeAllFilesBeingWritt
en
SEVERE: Failed to close file /tmp/test
org.apache.hadoop.ipc.RemoteException: org.apache.hadoop.hdfs.server.namenode.Le
aseExpiredException: No lease on /tmp/test File does not exist. Holder DFSClient
_NONMAPREDUCE_-1727094995_1 does not have any open files
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.checkLease(FSNamesystem.
java:1999)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.checkLease(FSNamesystem.
java:1990)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.completeFileInternal(FSN
amesystem.java:2045)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.completeFile(FSNamesyste
m.java:2033)
at org.apache.hadoop.hdfs.server.namenode.NameNode.complete(NameNode.java:805)
at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:587)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:1432)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:1428)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.ja
va:1190)
at org.apache.hadoop.ipc.Server$Handler.run(Server.java:1426)
at org.apache.hadoop.ipc.Client.call(Client.java:1113)
at org.apache.hadoop.ipc.RPC$Invoker.invoke(RPC.java:229)
at com.sun.proxy.$Proxy1.complete(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57
)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.io.retry.RetryInvocationHandler.invokeMethod(RetryInvocati
onHandler.java:85)
at org.apache.hadoop.io.retry.RetryInvocationHandler.invoke(RetryInvocationHand
ler.java:62)
at com.sun.proxy.$Proxy1.complete(Unknown Source)
at org.apache.hadoop.hdfs.DFSClient$DFSOutputStream.closeInternal(DFSClient.jav
a:4121)
at org.apache.hadoop.hdfs.DFSClient$DFSOutputStream.close(DFSClient.java:4022)
at org.apache.hadoop.hdfs.DFSClient.closeAllFilesBeingWritten(DFSClient.java:41
7)
at org.apache.hadoop.hdfs.DFSClient.close(DFSClient.java:433)
at org.apache.hadoop.hdfs.DistributedFileSystem.close(DistributedFileSystem.jav
a:369)
When I removed fs.close();, it worked well.
The environment is:
hadoop-core -- 1.2.1
jdk -- 1.6.0_21
What happened when filesystem closed?
Anyone has encountered this problem?
Generally, you should not call fs.close() when you do a FileSystem.get(...).
FileSystem.get(...) won't actually open a "new" FileSystem object. When you do a close() on that FileSystem, you will close it for any upstream process as well.
For example, if you close the FileSystem during a mapper, your MapReduce driver will fail when it again tries to close the FileSystem on cleanup.

sending sms from pc to mobile

i am using smsj api to send sms from pc to mobile through gsm modem. as given on the page, i have tried the following code.
package org.marre;
import java.io.IOException;
import org.marre.sms.SmsException;
public class SendMessage {
public void send() {
try{
// Send SMS with clickatell
SmsSender smsSender = SmsSender.getGsmSender("COM7");
String msg ="sample message";
// International number to reciever without leading "+"
String reciever = "9561356345";
// Number of sender (not supported on all transports)
String sender ="9561356345";
// Connect
smsSender.connect();
// Send message
smsSender.sendTextSms(msg, reciever, sender);
// Disconnect
smsSender.disconnect();
} catch(IOException i){
i.printStackTrace();
System.out.println("i");
} catch(SmsException s){
s.printStackTrace();
System.out.println("s");
}
}
public static void main(String args[]){
SendMessage app = new SendMessage();
app.send();
}
}
but i am getting this error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.marre.sms.transport.gsm.SerialComm.<clinit>(SerialComm.java:58)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.marre.sms.transport.gsm.GsmTransport.class$(GsmTransport.java:83)
at org.marre.sms.transport.gsm.GsmTransport.<clinit>(GsmTransport.java:83)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.marre.sms.transport.SmsTransportManager.getTransport(SmsTransportManager.java:75)
at org.marre.SmsSender.<init>(SmsSender.java:112)
at org.marre.SmsSender.getGsmSender(SmsSender.java:180)
at org.marre.SendMessage.send(SendMessage.java:12)
at org.marre.SendMessage.main(SendMessage.java:30)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 12 more
i am a beginner in java. please help me with the code.
note: i have used smslib api earlier for sending a simple text message. but this does not support ems messages. if you have any other library in mind which supports ems messages, please let me know. smsj was one i could easily download.
The real cause of the problem lies here:
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
The compiler couldn't find org.slf4j.LoggerFactory from the classpath.
Download SLF4J and add the library in your classpath and compile your code.
Note: As smsj references a very old version of Slf4J you need to use this old version as well: http://mvnrepository.com/artifact/org.slf4j/slf4j-simple/1.0-beta9

How to configure Jaql with an existing Hadoop Cluster and use jaql oprators to filter the result?

When I read a file from hdfs by giving it proper path the file is read successfully but when I try to use transform operator of jaql it throws an exception as given below and if I try to execute the code on JAQL shell then an exception is thrown of job.jar but even after adding the jar the exception is still thrown. if anybody knows that somehow JAQL is not configured properly with existing hadoop cluster or the exception is due to some other cause?
My code is:
jaql.setQueryString("read(lines('hdfs://hadoopserver:54310/dbreports/reports.json'," +
"{format: 'org.apache.hadoop.mapred.TextInputFormat',converter: 'com.ibm.jaql.io.hadoop.converter.FromJsonTextConverter'})) -> transform $.store_number;");
System.out.println("jaql running successfully....");
JsonValue jv = jaql.evaluate();
System.out.println("value is "+jv);
when run it throws an exception as:
Exception in thread "Thread-38" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpMethod
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:295)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.httpclient.HttpMethod
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at sbt.PlayCommands$$anonfun$61$$anonfun$63$$anon$2$$anonfun$loadClass$1.apply(PlayCommands.scala:563)
at sbt.PlayCommands$$anonfun$61$$anonfun$63$$anon$2$$anonfun$loadClass$1.apply(PlayCommands.scala:563)
at scala.Option.map(Option.scala:133)
at sbt.PlayCommands$$anonfun$61$$anonfun$63$$anon$2.loadClass(PlayCommands.scala:563)
... 1 more
java.io.IOException: Job failed!
..........
Do anybody knows what it is that i'm missing?

java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64 when trying to create CommonsHttpOAuthConsumer

EDIT:
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
System.out.println("debug1");
CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer("key","secret");
System.out.println("debug2");
...}
hi when i try to run this script i get this response:
debug1
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64
at oauth.signpost.signature.OAuthMessageSigner.<init>(OAuthMessageSigner.java:37)
at oauth.signpost.signature.HmacSha1MessageSigner.<init>(HmacSha1MessageSigner.java:30)
at oauth.signpost.AbstractOAuthConsumer.<init>(AbstractOAuthConsumer.java:65)
at oauth.signpost.commonshttp.CommonsHttpOAuthConsumer.<init>(CommonsHttpOAuthConsumer.java:30)
at mein.tester.main(tester.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 5 more
Line 21 is the line before
System.out.println("debug2");
Well i can easily compile the code in eclipse and i added 2 Signpost librabrys and also common-codec.jar as external library as i read in another post.. however, how do i make it available on runtime?(i did like in the post "add it to WEB-INF/lib " -> i created that folder and added the jar
-i also tryed the defaultoauthconsumer -> same error
Right click on the jar and choose "add to build path"? I don't think the ide will automatically add it to your classpath unless you tell it to.

Java applet with rxtx components for serial communication

I am trying to build an applet that can open a serial port and communicate with the same. I have used rxtxcomm.jar for the serial communications. I have an applet built that works in the eclipese environment perfectly. I built the Jar file and signed the same, but when run in the browser the console shows the foll:
java.lang.ExceptionInInitializerError thrown while loading gnu.io.RXTXCommDriver
Exception in thread "thread applet-zhas_xbeeComm.xtalk-1" java.lang.ExceptionInInitializerError
at zhas_xbeeComm.Xconnect$1.run(Xconnect.java:46)
at java.security.AccessController.doPrivileged(Native Method)
at zhas_xbeeComm.Xconnect.connect(Xconnect.java:40)
at zhas_xbeeComm.xtalk.init(xtalk.java:22)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission loadLibrary.rxtxSerial)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkLink(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:123)
... 6 more
I have even used doPrivileged method around the connect and open functions but it aint working! Please help!!
Here is a snippet of the code of the applet:
{
/** Function to open a port and begin reading and writing */
public void connect ( final String portName ) throws Exception
{
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
// privileged code goes here, for example:
// 1. added try catch for no such port exception;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(portName); //line 46
} catch (NoSuchPortException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Just had the same problem.
Please make sure that the first call to RXTX library is in doPrivileged block.
If it will try to load library before privileged block - it will fail with this error.
Some additional info:
http://hacky.typepad.com/blog/2009/05/using-rxtxcomm-in-applets.html

Categories