I am invoking the Git executable from my Java program using the process builder class and running various git commands. Java program is being run from the command prompt.
However, I'm not finding a way to extract only a particular part of the output.
I would assume you use a StreamGobbler thread to get the input.
Just put some logic in there that filters the output.
like the others I would recommend the use of a java library, JGit allows many low-level operations if you need something very precise.
But maybe the Git command you are looking for is : "git diff commit_parent commit -U0" giving only the changes and no context.
You can also read the manual: "git help diff", you will find what Git offers.
But once again, I do not think it is a better idea to use a java Git library: what if the Git developers decide to change the output format tomorrow? Will you need to start over your program?
Related
I'm using google-java-format to format Java code according to Google Java Style. However, I only find documentation and examples showing how to format one file using the CLI.
Is there a built-in way to format an entire Java project directly using the CLI (without looping using a shell script or something else)?
After a quick read of the google-java-format documentation,
it is intended to function one file at a time
or on a group of files,
each listed on the command line.
There appears to be plugins for intelliJ and Eclipse.
If you need to format every file in your project,
you will need to do one of the following:
Feed a list of every file. This is fairly easy with a script; use xargs.
If you use IntelliJ, check the plugin options. It likely has some kind of selection mechanism.
As above, the Eclipse plugin likely has some kind of selection mechanism, as well.
I have a task to implement a program in Java (pure Java without 3rd party libraries) that reads a history of any git repository and puts the commits into tree data structure.
Could you give me any hints? How to read git log in Java without 3rd party libraries?
You might want to take a look at Processes and Threads and how to execute a command in the runtime. It does have some details and need fundamental understanding of java.lang.Runtime, java.io and some other relevant topics, so that I'd refrain to write a whole method here and recommend you to search for a good tutorial and also get the first idea from other questions here, like → getting output from executing a command line program
I have a java application(runnable jar) and VB scripts which I'm using to telnet to a remote machine and executing some cmds. So, I first execute the vbs files and then run my jar(in all everything is working fine).
But, now I want to integrate scripts and my java jar such that, running the jar should first trigger the script followed by Java related task.
Few thing which I've come across are -
I cannot trigger Vbs from Java(javax.script - correct me if I'm wrong). So, possible options to rewite the script in are -
Javascript(have no idea what my Javascript file would have so that after reading it inside java class I can write it to the Socket output stream.)
PHP(I tried this using Java bridge but it gives some error saying cgi needs to installed. And, I believe it also requires PHP to be installed on the host machine before executing my jar. So I'm not going any futher with this approach.)
Long story short, I don't want to create any dependencies - I am looking for something like where I can package any external lib with my jar(if required) and use it to execute my scripts.
You can execute the VB-Script in an external command. There are a lot of resources on the internet that explain how to do that - for instance this link also explains how to start a VB-Script from within java. However I do not know if you need the output from the script within the Java. If so you'll have to listen to the outputstream of the created process. You should find an example for that as well with that link (using the processbuilder)
If you have the script packaged within your jar, I fear you'll have to unpack it to a temporary folder and execute it there.
The closest I have seen about VB script as a JVM language is in answer here.
Visual Basic or VBScript as Java Scripting Engine
Have you seen this wikipedia entry about JVM languages?
http://en.wikipedia.org/wiki/List_of_JVM_languages
Also, have you considered using Ant and using it programmatically from java?
Another option is to use groovy/Ant from Java.
I need to create a Java command line to that will be invoked remotely on a server. It will need to:
Read "lines" of text from the user.
Recognise if the user presses the "tab" key to facilitate auto-complete.
Recognise if the user presses the "up/down" keys for history.
Before I go off and roll my own, Is anyone aware of a Java library that might facilitate all or part of this?
i.e. From the command line in ssh it might look like this:
bob> java -jar MyTool.jar
MyTool Started.
Please enter command:
> server1 startup
server starting...
server started
> server2 load accounts
Done
> server3 shutdown
Complete
>quit
Check out JReadline and jline2.
Update: picocli-shell-jline2 and picocli-shell-jline3 combine the strength of JLine 2 and 3 with picocli.
Picocli allows you to write commands (and subcommands) with options and positional parameters using very little code, and the library will generate JLine 2 and 3 command completers for all commands.
It uses ANSI colors and styles in the usage help message, and has many other unique features like negatable options, repeatable nested argument groups, variable interpolation and more.
Disclaimer: I am the author and therefore biased.
it seems like you're trying to ask 3 different questions at once and don't know what you really want an answer to.
accepting user input and providing auto-complete is trivial and i highly doubt you will find a generalized library for such a task
parsing complex bash-like statements sounds like something cool to have and a library may exist to do that, but i don't think it would give you much headroom to create your own set of bash-like instructions. (especially considering you say it needs to be more sophisticated than anything you could do as a bash script - which is a tall order)
parsing simple user input as if it was a command-line input or command is also rather trivial, and if this is what you are looking for, you should look at this possible duplicate: How to parse command line arguments in Java?
i recommend restructuring your question to be more specific in exactly what you are looking for and to avoid putting emphasis on the trivial task of "auto-complete" and simply accepting the users input in a text box.
Have you taken a look at the BeanShell? It doesn't act like a shell proper (like bash or csh) but it'll let you type java commands like an interpreter and you can use tab to autocomplete.
I've only used the 1.X versions of bean shell but they always open a window for you so it's not something you can run inside an existing shell.
I assume you mean something similar to the python interpreter. The reason there is not equivalent in Java is because Java needs to be compiled to bytecode before it can be executed.
If you are looking for something with good auto-complete capabilities. I would recommend eclipse or netbeans. They also compile your application automatically, allowing you to quickly run your code once you are done writing it.
Hope that helps.
Bit late to the party on this one, but I'd add Crash and Cliche to the mix.
Apache Karaf has a command shell which can be used as a library to build a command shell for other applications. You create classes to represent commands, and use annotations to specify the options to the commands. The Karaf library provides tab completion, history and line editing, and the ability to run interactively or read in files or command line arguments to execute as a script.
I found out about it here and have used it in my own project; it works quite well. I can't compare it to any of the others as I haven't used them.
I'm a java programmer. I use bash scripts a lot for text processing.
Utilities like grep,sed,awk,tr,wc,find, along with piping between commands gives such a powerful combination.
However bash programming lacks portability, testability and the more elegant programming constructs that exist in Java. It also makes it harder to integrate into our other Java products.
I was wondering if anyone knows of any Java text processing libraries out there which might offer what I'm looking for.
It would be so cool to be able to write:
Text.createFromFile("blah.txt).grep("-v","ERROR.*").sed("s/ERROR/blah/g").awk("print $1").writeTo("output.txt")
This might be pie-in-in-the-sky stuff. But thought I'd put the question out there anyway.
Unix4j implements some basic unix commands, mainly focussing on text-processing (with support for piping between commands): http://www.unix4j.org
Example (Ben's example, but without awk as this is not currently supported):
Unix4j.fromStrings("1:here is no error", "2:ERRORS everywhere", "3:another ERROR", "4:nothing").toFile("blah.txt");
Unix4j.fromFile("blah.txt").grep(Grep.Options.v, "ERROR.*").sed("s/ERROR/blah/g").toFile("output.txt");
Unix4j.fromFile("output.txt").toStdOut();
>>>
1:here is no error
4:nothing
Note:
the author of the question is involved in the unix4j project
Believe it or not, but I used embedded Ant for many of those tasks.
Update
Ant has Java api's that allow it to be called from Java projects. This is embedded mode. This is a reference to And Api 1.6.1. Distribution should include docs as well.
To use it, you would create new task object, set appropriate parameters and execute it just as you would in build.xml but via Java Api. Than you can run your task.
Something like
ReplaceRegExp regexp = new ReplaceRegExp();
regexp.setMatch("bla");
regexp.setFile(new File("inputFile"));
regexp.execute();
You may need to set up some other stuff as well.
Not sure if it solves your problem, but Ant has a lot of code to do things. Just search through docs.