What is the purpose of proto_path in the protoc command - java

My bash script has the following code generate invocation for "Protocol Buffers".
protoc --proto_path=src --java_out=src
src/com/domain/project/persistentThing.proto
That was from 2012. Today the online documentation for Java Protocol Buffers says:
protoc -I=$SRC_DIR --java_out=$DST_DIR
$SRC_DIR/addressbook.proto
This makes it seem like --proto_path is superceded. What was the purpose of --proto_path?

-I and --proto_path are the same flag: -I is the shorthand version.
The purpose of the flag is to specify a directory in which to look for imported files, much like the C compiler's -I flag, Java's CLASSPATH environment variable, Python's PYTHONPATH, etc. You may specify multiple directories by passing multiple -I flags; they will be searched in order.

Related

run java jar file on HP-UX as UNIX as "os.name"

I need to execute a jar file on HP-UX that I am not supposed to modify.
I unpacked it using jd-gui and found out that I am failing cause in java there is a condition to check the os, leading to different directions for win, macos, freebds, openbds, gnu and so on.
I am quite sure everything would work if I would be able to make my unix command line reply freebds or openbds to the java call
System.getProperty("os.name")
once executed from a jar file like:
java -jar myjar.jar
is there a way to achieve this? some kind of compatibility mode or a way to preset that parameter.
You can use the -D switch to specify system properties. In my experiment this (unexpectedly) even worked with pre-defined ones like os.name. Therefore this should work:
java -Dos.name=linux -jar myjar.jar

Access classes defined in a jar file, from within (for instance) JRuby irb?

(Crossposting note: I have asked this question one week ago at the JRuby mailing list, but didn't get any answer yet).
I have a jar file provided by someone else, no access to the source code. The jar file is in lib/other/appl.jar, the class is named Appl, and the package is com.abc.xyz
I would like to instantiate an Appl object from the JRuby irb, jirb_swing_ex.
(Of course my problem applies not only to jirb, but to running JRuby programs in general, but I explain it in the way I'm using it right now, just in case there are some peculiarities in Jirb which need special treatment).
THIS is the way it DOES work:
(1) Invoke jirb by:
java -jar jr/jruby-complete-1.7.27 jb/jirb_swing_ex
(2) Put the directory with the jar file into the load path:
$: << 'lib/other'
(3) Load the jar file
require 'appl.jar'
(4) Import the class
java_import com.abc.xyz.Appl
(5) Create the object
x = Appl.new
As I said, this works, and I can live with it if necessary, but I would prefer a simpler approach:
NOW TO MY QUESTION: Instead of fiddling around with load path and doing a require for the Jar file, I thought I could let Java already include the jar file. This is what I have tried:
java -cp lib/other/appl.jar -jar jr/jruby-complete-1.7.27 jb/jirb_swing_ex
The problem is: How can I get at my object? If I just use the class name com.abc.xyz.Appl, JRuby complains that the class not found (NameError: missing class name).
BTW, I have also tried forward slashes (since I'm on Windows), i.e.
java -cp lib\other\appl.jar -jar jr\jruby-complete-1.7.27 jb\jirb_swing_ex
but the same effect. I had expected that, having appl.jar in my class path, would make the classes available somehow, but I seem to miss something.
Running jirb or jirb_swing with custom class path
jirb and jirb_swing will use the value of JRUBY_CP environment variable (if present) to extend class path given to Java command line.
Example with commons-lang3 library taken from my local maven repository, using bash on Linux or macOS:
$ export JRUBY_CP=${HOME}/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar
$ jirb
irb(main):001:0> org.apache.commons.lang3.mutable.MutableBoolean.new
=> #<Java::OrgApacheCommonsLang3Mutable::MutableBoolean:0x7c24b813>
Running JRuby programs with custom class path
To run a JRuby program that uses a third-party java library, this won't work:
java -cp lib/other/appl.jar -jar jr/jruby-complete-1.7.27 ...
You must use either -jar or -cp, you can't combine the two.
From java man page:
When you use this option [-jar], the JAR file is the source of all user classes, and other user class path settings are ignored.
In addition, you need to pass the main Java class, which is org.jruby.Main, and that class needs arguments: either a path to a Ruby script, or other command line arguments such as -e 'puts 2+2'.
So the command line structure is the following:
# Run script file:
java -cp path/to/jruby.jar:other/custom.jar org.jruby.Main path/to/script
# Run simple one-line Ruby program
java -cp path/to/jruby.jar:other/custom.jar org.jruby.Main -e 'some ruby here'
(on Windows please use ; instead of : as separator)
Actual example with same commons-lang3 library & OS:
$ alias myjruby="java -cp ${JRUBY_HOME}/lib/jruby.jar:${HOME}/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar org.jruby.Main"
# Verifying base jruby code works with that:
$ myjruby -e 'puts 2+2'
4
# Now verifying it can use my 3rd-party lib:
$ myjruby -e 'puts org.apache.commons.lang3.mutable.MutableBoolean.new'
false

Excluding period for terminal autocompletion

I have a slight pet peeve when I try to compile and run my java programs in the terminal
javac example.java
When I want to run java example and use autocomplete on the java command, it automatically includes the period (java example.). Of course, this is standard behavior as it matches the longest prefix, but is there a way to cut off the trailing period here? I wouldn't mind editing certain configuration files and I believe I saw other computers slicing off the period. It would be especially nice if this functionality is exclusive to the terminal command java.
As comments stated, the answer was to use bash-completion. I used the following git-repo as a reference. As for my case on a Mac (taken straight from the link), the instructions are as follows:
brew install bash-completion
and inside ~/.bash_profile, the following lines are added:
if [ -f `brew --prefix`/etc/bash_completion ]; then
. `brew --prefix`/etc/bash_completion
fi

Difference between running with Java & Scala [duplicate]

Can command java run a compiled scala code? If so, why do we have an exclusive command scala?
You can run byte code generated by Scala if you include all necessary runtime libs for Scala (scala-library.jar, scala-swing.jar ...) in the classpath. The scala command does this automatically, and supports Scala specific command line arguments.
Yes, it can. Scala is compiled down to Java bytecode. But remember that it depends on the Scala runtime classes, so you need to still have Scala's jar files on the classpath.
If so, why do we have an exclusive command scala?
Convenience wrapper.
Scala is designed to integrate easily
with applications that run on modern
virtual machines, primarily the Java
virtual machine (JVM). The main Scala
compiler, scalac, generates Java class
files that can be run on the JVM.
-> http://www.artima.com/scalazine/articles/steps.html
As long as you have installed the scala runtime you should be fine: compile classes with scalac and run them with java.
Just want to add my own answer as additional value for the future readers:
scala, if run without parameter, will run an interactive shell
scala, if run with a text file name as parameter, will regard the file as a scala script
those two can't be done using java
If you look closely, the scala command is simply a bash helper-script which summarize to the below command:
$cat /usr/local/Cellar/scala#2.11/2.11.12_1/libexec/bin/scala
execCommand \
"${JAVACMD:=java}" \
$JAVA_OPTS \
"${java_args[#]}" \
"${classpath_args[#]}" \
-Dscala.home="$SCALA_HOME" \
$OVERRIDE_USEJAVACP \
"$EMACS_OPT" \
$WINDOWS_OPT \
scala.tools.nsc.MainGenericRunner "$#"
There are 2 things required to run a .class file compiled using scalac ( the scala compiler) using the java command.
We need to include the scala-library.jar and the location of the .class file in the classpath. To find the location of scala-library.jar, please execute the
below:
which scala /usr/local/opt/scala#2.11/bin/scala
In my case the scala-*.jar files are in :
/usr/local/Cellar/scala#2.11/2.11.12_1/idea/lib on Mac
The location of the Main2.class file which is in /training/example1/scala.
So, to execute the program we could use the below command:
java -cp /usr/local/Cellar/scala#2.11/2.11.12_1/idea/lib/scala-library.jar:/training/example1/scala/ Main2
EDIT-1: If you are using windows, please use semicolon(;) as the separator in java classpath command.

Parsing javadoc with Python-Sphinx

I use a shared repository partly containing Java and Python code. The code basis mainly stands on python, but some libraries are written in Java.
Is there a possibility to parse or preprocess Java documentation in order to use
it later in Python-Sphinx or even a plugin?
javasphinx (Github) (Documentation)
It took me way to long to find all the important details to set this up, so here's a brief for all my trouble.
Installation
# Recommend working in virtual environments with latest pip:
mkdir docs; cd docs
python3 -m venv env
source ./env/bin/activate
pip install --upgrade pip
# Recommend installing from source:
pip install git+https://github.com/bronto/javasphinx.git
The pypi version seemed to have broken imports, these issues did not seem to exist in the latest checkout.
Setup & Configuration
Assuming you've got a working sphinx setup already:
Important: add the java "domain" to sphinx, this is embedded in the javasphinx package and does not follow the common .ext. extension-namespace format. (This is the detail I missed for hours):
# docs/sources/conf.py
extensions = ['javasphinx']
Optional: If you want external javadoc linking:
# docs/sources/conf.py
javadoc_url_map = {
'<namespace_here>' : ('<base_url_here>', 'javadoc'),
}
Generating Documentation
The javasphinx package adds the shell tool javasphinx-apidoc, if your current environment is active you can call it as just javasphinx-apidoc, or use its full path: ./env/bin/javasphinx-apidoc:
$ javasphinx-apidoc -o docs/source/ --title='<name_here>' ../path/to/java_dirtoscan
This tool takes arguments nearly identical to sphinx-apidoc:
$ javasphinx-apidoc --help
Usage: javasphinx-apidoc [options] -o <output_path> <input_path> [exclude_paths, ...]
Options:
-h, --help show this help message and exit
-o DESTDIR, --output-dir=DESTDIR
Directory to place all output
-f, --force Overwrite all files
-c CACHE_DIR, --cache-dir=CACHE_DIR
Directory to stored cachable output
-u, --update Overwrite new and changed files
-T, --no-toc Don't create a table of contents file
-t TOC_TITLE, --title=TOC_TITLE
Title to use on table of contents
--no-member-headers Don't generate headers for class members
-s SUFFIX, --suffix=SUFFIX
file suffix (default: rst)
-I INCLUDES, --include=INCLUDES
Additional input paths to scan
-p PARSER_LIB, --parser=PARSER_LIB
Beautiful Soup---html parser library option.
-v, --verbose verbose output
Include Generated Docs in Index
In the output directory of the javasphinx-apidoc command there will have been a packages.rst table-of-contents file generated, you will likely want to include this into your index.html's table of contents like:
#docs/sources/index.rst
Contents:
.. toctree::
:maxdepth: 2
packages
Compile Documentation (html)
With either your python environment active or your path modified:
$ cd docs
$ make html
or
$ PATH=$PATH:./env/bin/ make html
The javadoc command allows you to write and use your own doclet classes to generate documentation in whatever form you choose. The output doesn't need to be directly human-readable ... so there's nothing stopping you outputting in a Sphinx compatible format.
However, I couldn't find any existing doclet that does this specific job.
References:
Oracle's Doclet Overview
UPDATE
The javasphinx extension may be a better alternative. It allows you to generate Sphinx documentation from javadoc comments embedded in Java source code.
Sphinx does not provide a built-in way to parse JavaDoc, and I do not know of any 3rd party extension for this task.
You'll likely have to write your own documenter for the Sphinx autodoc extension. There are different approaches you may follow:
Parse JavaDoc manually. I do not think that there is a JavaDoc pParser for Python, though.
Use Doxygen to parse JavaDoc into XML, and parse that XML. The Sphinx extension breathe does this, though for C++.
Write a Doclet for Java to turn JavaDoc into whatever output format you can hande, and parse this output.

Categories

Resources