Using m4 macros with Eclipse & Java - java

Is there a way to use m4 macros when developing in Java for Eclipse, ie. making sure the preprocessor is automatically invoked before Eclipse compiles?
Or has anyone used another preprocessor successfully with Eclipse?

You can specify an arbitrary builder on an Eclipse project, and order the builder so that it is executed before the Java builder is run.
To define the new builder, open the project properties (right click->Properties or alt-enter), select Builders then New.... Select Program, then in the dialog configure the builder (hopefully you know what needs to be done here) and select OK. Back in the Builders page you can then select your new builder and select Up until it is before the Java Builder

I think I have a way.
I just now got it working with the c preprocessor in eclipse on windows. I'm sure it could be adapted to M4, but I DO use gnu CPP's ability to create make files expressing the dependencies of a file. Doing that for m4 would be up to you. It has a few problems. If there's an error during preprocessing eclipse ignores it and keeps going. Also if you built with "run" the preprocessor's console output disappears as soon as the program starts.
Since I don't understand ant, and I don't have time to learn, my version is based on using Ruby for glue. I wrote a little automake in Ruby that takes a list of files that have been touched before the last make, filters out the ones that are preprocessed files, and those that are preprocessor includes, scans untouched preprocessor files to see if they depend on anything that has been changed, then processes them all.
These all depend on using Eclipse's "builders" which only seem to work in Juno.
In my case the setting for the project are:
create a builder for the project settings that runs before the java builder
set the location to: C:\Ruby192\bin\ruby.exe
set the working directory to C:\Ruby192\bin\
set the arguments to: C:\preprocessors\mymake.rb ${build_project} ${build_files:acf}
this passes the directory of the project followed by the file that have been touched
set "refresh" to "project containing the selected resource" and "recursively include subfolder"
set build options to: Allocate console
Run the builder: after clean,during manual builds, and during autobuilds
Note that according to my simple make ".jpp" files will be processed into ".java" files
and ".jpp" files can #include ".jph" files (and only .jph files)
".jph" files can also #include ".jph" files and only ".jph" files
All of these files have to be under the /src/ directory (recursively in directories under /src/ which is how eclipse organizes java code and packages).
Here's the Ruby code:
require 'set'
$path = (ARGV[0].gsub('\\','/')+'/src')
$process=(ARGV[1..-1]||[]).map{ |a| a.gsub('\\','/') }
def pending(ending)
($process.select do |a|
a[0..$path.length-1]==$path && a[-4..-1].downcase == ending
end).to_set
end
def read_make_dependencies(filename)
((File.open(filename).read.
split("\n")[1..-1].
map do |a|
if a[-2..-1]==' \\'
a=a[0..-3]
end
a.lstrip.gsub('\\/','/').gsub('\\','/').rstrip
end)||[]).to_set
end
$pendingJph=pending('.jph')
$pendingJpp=pending('.jpp')
Dir.chdir($path)
$untouchedJph=Dir['**/*.jph'].map{|a| $path+'/'+a}.to_set - $pendingJph
$untouchedJpp=Dir['**/*.jpp'].map{|a| $path+'/'+a}.to_set - $pendingJpp
Dir.chdir('C:/MinGW/bin')
$pendingJph.each do|a|
o = a[0..-4]+'depend'
system "cpp -MM \"#{a}\" -o \"#{o}\""
$pendingJph = $pendingJph + read_make_dependencies(o)
end
$untouchedJpp.each do|a|
o = a[0..-4]+'depend'
system "cpp -MM \"#{a}\" -o \"#{o}\""
if not (read_make_dependencies(o) & $pendingJph).empty?
puts "touching #{a}"
$pendingJpp << a
end
end
$pendingJpp.each do|a|
o = a[0..-4]+'java'
puts "processing #{a}"
system "cpp -w -P -C -x c \"#{a}\" -o \"#{o}\""
end

Related

Running a java program, having external jar dependency in a batch file

I tried googling a lot but couldnt get a proper working solution ..
directory consists of all java files and external jarfile(google.guava.jar).. i want to execute it in a batch file.. i have tried a lot of things...but still says deffclasserror.. can anyone help me out on how to make it work...(Windows)..
Structure looks like this:
Folder
--------jar file
--------java file
--------bat file
set path="C:\Program Files (x86)\Java\jdk1.8.0_73\bin"
javac -cp google.guava.jar convertohash
javac FinalOutput.java
java convertohash
java FinalOutput
pause
Try this:
"C:\Program Files (x86)\Java\jdk1.8.0_73\bin\java" -cp %YOUR_CLASSPATH%;%YOUR_CLASSPATH_REPORTS%;%EXTRA_LIB% -Djava.library.path=./dll your.main.class
Before this line you need to set up your YOUR_CLASSPATH your YOUR_CLASSPATH_REPORTS and EXTRA_LIB with = and separating the concurrences with ";" (without the ""). For example:
SET EXTRA_LIB=.\lib\mysql-connector-java.jar;.\lib\anotherlibrary.jar; etc
Being the "lib" folder the one were you store your libraries, the route doesn't stricly needs to be the one shown on the example just put the one were you store your libraries (if you are using some ofc).
Also keep in mind that if you are going to use this bat in several machines they must have the same jdk installed and on the same route specified or you'll need to change it manually because the application wont launch.

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.

Where does HP Fortify put the intermediate files?

According to the HP Fortify documentation, the Static Code Analyzer first translates the source code into an intermediate format, and then it scans the translated code and generates a vulnerability report.
It says the translation can be done using the following Ant code:
<antcall target="compile">
<param name="build.compiler" value="com.fortify.dev.ant.SCACompiler"/>
</antcall>
This will call your "compile" target but force it to use the SCACompiler instead of the regular javac compiler.
I have run Fortify on our Java code and it produces vulnerability reports. But I do not see the intermediate files anywhere. I ran a diff between the Java class files that the regular javac compiler produced and the Java class files that the SCACompiler produced, and they were exactly the same. Are the intermediate files stored somewhere else, or does Fortify automatically delete them after performing the scan?
The intermediate files are not class or object files. They are NST (Normalized Syntax Tree) files, a proprietary format used by HP Fortify (this is discussed in the book "Secure Programming with Static Analysis". When translating with a build ID, such as:
sourceanalyzer -b test ant
Then it will be stored in the project working directory. In Windows, typically:
%USERPROFILE%\AppData\Local\Fortify\sca<version>\build\test
or on other platforms:
~/.fortify/sca<version>/build/test
this will then contain the canonicalized path to the NST, as was performed during the translation. These can then be used to scan multiple times if needed, but should be "cleaned" if scanning a separate new (or updated) codebase.
For ant integration I think it depends on which version of Ant, and the way you are translating, but this way I think it just calls the sourceanalyzer.jar file (which contains the com.fortify.dev.ant.SCACompiler class) in order to hook into the JVM and follow the build to create the NST files needed for scanning. I don't believe it's actually a separate version of javac, although perhaps there is a separate version under <SCA installation directory>/jre/ which it may use.
Lavamunky is correct about the default path for the working directory. You can change this in the following locations:
1. FortifyInstallRoot\Core\config\fortify.properties: com.fortify.WorkingDirectory
2. FortifyInstallRoot\Core\config\fortify-sca.properties: com.fortify.sca.ProjectRoot
Note that you need to use / as the path delimiter instead of \ inside of the config files. Inside of the folder specified by those paths, the pattern is: sca\build\.
You can also specify these at runtime:
sourceanalyzer -b MyBuild -Dcom.fortify.WorkingDirectory=C:\Fortify\Work -Dcom.fortify.sca.ProjectRoot=C:\Fortify\Work
The path to the working files would then be:
C:\Fortify\Work\sca<version>\build\MyBuild\

How can I run different projects from a script in windows?

I have two visual studio projects (for different purpose... they act on two different hardware) and also, I have a Java project that have control on something else. I am working in windows. My question is that how can I write a script (like shell script) to control different projects from a single program. More specifically, suppose I want to do something like this: execute project 1 with parameters x1,x2,...,xn, then execute project 2 with parameters y1,y2,...,yn, then execute project 1, then project 3, & so on...
Is there any tutorial or short description that I can follow to implement my concept?
There's a myriad of options/scripting languages/... to achieve this, but if you just want to run a bunch of exes then a simple windows batch file will do just fine:
#echo off
set my_exe=c:\path\to\my.exe
set my_other_exe=c:\path\to\my_other.exe
%my_exe% a
%my_exe% a b c
%my_other_exe% x
%my_other_exe% x y z
pause
Save this with your favourite editor with a .bat extension and run by double-clicking. The lines with set create variables so you don't have to paste the full path to your exes the whole time. The lines referring to those variables will invoke the executables aith the argumenst that come after it. Search the internet for 'batch file examples' or something like that and you will quickly find all the info needed.

Prevent Eclipse Java Builder from Compiling Java-Like Source

I'm in the process of writing an eclipse plugin for my programming language Whiley (see http://whiley.org). The plugin is working reasonably well, although there's lots to do. Two pieces of the jigsaw are:
I've created a "Whiley Builder" by subclassing incremental project builder. This handles building and cleaning of "*.whiley" files.
I've created a content-type called "Whiley Source Files" for "*.whiley" files, which extends "org.eclipse.jdt.core.javaSource" (this follows Andrew Eisenberg's suggestion).
The advantage of having the content-type extend javaSource is that it immediately fits into the package explorer, etc. In principle, I could fleshout ICompilationUnit to provide more useful info, although I haven't done that yet.
The disadvantage is that the Java builder is trying to compile my whiley files ... and it obviously can't. Originally, I had the Java Builder run first, then the Whiley builder. Superficially, this actually worked out quite well since all of the errors from the Java Builder were discarded by the Whiley Builder (for whiley files). However, I actually want the Whiley Builder to run first, as this is the best way for me to resolve dependencies between Java and Whiley files.
Which leads me to my question: can I stop the Java builder from trying to compile certain java-like resources? Specifically, in my case, those with the "*.whiley" extension. As an alternative, I was wondering whether my Whiley Builder could somehow update the resource delta to remove those files which it has dealt with.
Thoughts?
A quick way:
- Right-click on the offending item (Can be a file or folder)
- Select Java Build Path > Exclude
This adds an "exclude pattern" to your project the "pattern"
is the exact path to the file or folder you right-clicked on.
To add a general pattern you can do:
- Project > Properties > Java Build Path > Source
- Click on "Excluded"
- Click on "Edit"
- Click on "Add"
I like to add these exclude patterns:
**/*.Old
**/Old/ Trailing slash is required
These will get Eclipse to ignore all files with ".Old" extension
and all folders called "Old".
Source : http://www.davekb.com/browse_programming_tips:eclipse_exclude_ignore_certian_files_extensions:txt
I'm not a Java guy so pardon the stuidity on this answer...
If you wrote the whiley builder can't you just change the "whiley spec" so that all whiley code must be enclosed in a Java comment block... there by preventing eclipse from compling it?

Categories

Resources