groovy require load source file - java

I have my java code running.
I added a groovy shell to evaluate the main groovy file.
What it does is simple.
GroovyShell.run (main.groovy)
Now, in my main.groovy, if I have other .groovy files I'd like to "require", how can I do that?
Is there something like "require, source, load, require_one, import" filename?

http://groovy.codehaus.org/Embedding+Groovy
if you scroll down to the section entitled "Dynamically loading and running Groovy code inside Java" you will see a full example with two different approaches to solving your problem

Actually, here is the answer:
I couldn't find a function like that in Groovy.
So, when evaluating your file, you have to parse all groovy files.
If you add new files on the fly and would like to evaluate them, you can't reuse the shell, not even through a singleton via the evaluated script
What you can do is register your parsed files and force the shell to "re-parse" them all again when re-evaluating your file.

Related

How to override explicitly defined Java source files for Javadoc using additional options files?

I have some tool creating an options file for Javadoc containing lots of individual Java source files to process. That tool simply adds all Java files automatically and allows me to add additional options files to the process created manually. The goal is to use such an additional options file to make Javadoc ignore some of the explicitly defined source files.
The first automatically generated options file looks like the following:
-classpath '[...]'
-d '[...]'
-doctitle '[...]'
'C:\\Users\\[...]\\package-info.java'
'C:\\Users\\[...]\\[...].java'
[...]
It contains a lot more paths for each individual Java-file in my project of course. The tools then invokes Javadoc the following way, where all but the first options file is the one I have control over:
javadoc #optsFile1 #optsFile2 #optsFile3
So, is it possible at all to somehow override the explicit paths of the first file using some options in the later files only?
I already tried various combinations of -exclude and -subpackages, but none of them worked. Javadoc always seems to process the explicitly defined files of the first and as well outputs their HTML. I don't care about unnecessary processing those files as well, I only don't want all of their HTML in the output folder. Would be great to have some option to post-filter things based on package names, because I would like to avoid to deal with paths.
Thanks!

Trouble Understanding/Locating a Jar file (JIDT package) needed for Octave to Java Array Conversion

In this previous question, I was trying to rework some Matlab code and figure out a package called javaplex to be compatible with Octave; it uses Java, but is tooled for Matlab, hence that issue. Now in an interval of time, I was busy/running simulations, and hadn't gotten around to a final step - actually using the package, with most all of the difficulties worked out. It turns out that another step exists: I need to convert an Octave array to a Java array (although I'm not sure why this issue didn't come up in Matlab).
To do so, I have turned to this script, in which the comments indicate that when using it, it
Assumes the JIDT [Java Information Dynamics Toolkit] jar is already on the java classpath - you will get a java classpath error if this is not the case.
So I go to the JIDT GitHub page and download this package. Now I am not a very avid user of java, so I believe I am failing to see something fairly straightforward: I am not sure where the "JIDT jar" is that is referenced in the above block quote! I can't find such a particular jar file to put in Octave's java classpath. In this tutorial for JIDT, they say you need the "infodynamics.jar" file in the classpath (page 9). I'm not sure what jar file I should be looking for, and where. Any help understanding the nature, name and location of this jar file (within the infodynamics toolkit folder) would be appreciated!
As an inevitable follow-up question, because this will come up upon resolving this issue, I would like to clarify the following procedure is how to add a jar file to the Octave (static) java classpath (following this answer here, I wasn't sure if I was implementing correctly):
I create a file called "javaclasspath.txt" inside of the directory I use in Octave.
I enter the name of files as follows: "./path/to/your-file.jar"
I suppose my main issue here is where do I start the path (all the way back with "C:/..."?), and do I put this "javaclasspath.txt" file in the directory folder I will be using most of the time in Octave?
Edit: I cannot find "infodynamics.jar" as shown here:
The JIDT jar is named infodynamics.jar and it is located in the root of the downloads infodynamics-dist-1.4.zip file.

Write a batch file that starts a java program

So I have a java project with multiple java files.
I know that is almost straight forward to start a java application using batch file. But that is for a pretty simple java program with a single class.
However I am wondering if it is possible to do that with in a scale of a project that you usually create using eclipse. A large project with multiple packages, classes and multiple java files.
My try was to write a script and apply on the main class as following
set path = C:\Program Files\Java\jdk1.7.0_25\bin
javac -classpath twitter/twitter4j-stream-3.0.5.jar;twitter4j-core-3.0.5.jar" sourcepath="lib/twitter4j-core-4.0.1.jar;lib/twitter4j-core-4.0.1.jar;lib/twitter4j-stream-4.0.1.jar;svm_light_lib Program.java
java Program
However when I start the .bat file it automatically closes.
Any Ideas ?
Thanks in advance
First, never overwrite the environment variable path, not even
temporarily. Append your folder instead: set "path=%path%;%mypath%" or set "path=%mypath%;%path%".
(There exists a particular path command but I'm not sure about right syntax: path=%path%;%mypath% with = assignment or path %path%;%mypath% without it).
Use full path to a program if you know it, e.g. "%mypath%\javac".
For better readability, values for -classpath and -sourcepath options are stored to the environment variables mycpth and mysrcp, respectively. Note and use proper " quotation and no spacing around = to avoid any leading and trailing spaces in all set commands.
pause to see all the javac output. Displays the message Press any key to continue . . .
Next code should be (syntax) error-free. However, success depends (among others) on classpath and sourcepath entries visibility as well...
set "mypath=C:\Program Files\Java\jdk1.7.0_25\bin"
set "path=%path%;%mypath%"
set "mycpth=twitter/twitter4j-stream-3.0.5.jar;twitter4j-core-3.0.5.jar"
set "mysrcp=lib/twitter4j-core-4.0.1.jar;lib/twitter4j-core-4.0.1.jar;lib/twitter4j-stream-4.0.1.jar;svm_light_lib"
"%mypath%\javac" -classpath "%mycpth%" -sourcepath "%mysrcp%" Program.java
pause
java Program
However I am wondering if it is possible to do that with in a scale of a project that you usually create using eclipse. A large project with multiple packages, classes and multiple java files.
Of course it is possible!
In this case, I suspect the problem is that you java command doesn't have a "-cp" argument. The java command is probably failing because it can't find twitter classes ... at runtime.
Remember to include "." on the classpath ... or else java won't find the file that you just compiled.
#JB Nizet's suggestion is also very important advice for finding out what is actually happening.

Is there a way to pass ant variables to java code (android)?

I like to include some build variables in my application. For example a buildnumber. Can I pass some variables from a ant build script to my Android application and use that variable in java?
Anybody has a link or example?
Use an ant task (such as replaceregexp) to insert the build numbers and other variables into a class for that purpose.
<replaceregexp file="${my.version.class.file}" match="#version#" replace="#${build.number})#" />
Or something similar. Basically you want to compile the information into a class as part of the build process.
Just populate the build number into a string xml file and read it as normal. You can use the replace task to do that..
Dont use a properties file since there is not Android native way to read it. The native way are string values in the xml files. And it is better to replace it into a static strings xml file rather than java source code too.
One thing you can do is to first use the "echo" task to create a properties file. That task supports variable substitution, so you can include all build variables. Then include that properties file in your application jar and use the Java Properties to read them.
See:
http://ant.apache.org/manual/Tasks/echo.html
http://download.oracle.com/javase/6/docs/api/java/util/Properties.html

How to "Export" code the right way?

I have a project in Java and I need to make a code listing in part of my LaTeX documentation with all my classes and code. What is the best way to export the code? Is it simply just copy and paste, or is there a way to export the code properly to keep all the formatting?
It's easy enough to do:
for d in `find <projectdir> -name '*.java'; do
echo "$d" >> output.txt
cat "$d" >> output.txt
done
...but what possible purpose could dumping all the code into a document serve?
If LaTeX is the target, I'd search for some formatting templates for code. It's easy enough to get text into LaTeX, but the formatting will be a different matter.
I found a way:
http://texblog.wordpress.com/2008/04/02/include-source-code-in-latex-with-listings/
although, this way the code seems to run off the page..
You can also try the package listings you mention directly within Eclipse with the TEXlipse plugin
(source: sourceforge.net)
You can then see if you reference the right Java files in your references.
Used in this tutorial slide 19.
You can also try :
GNU source-highlight, which can produce Latex output, and may be more appropriate for batch processing a all lot of files.
pygmentize, which needs Python, and should also produce Latex output.

Categories

Resources