I'm trying to do soemthing which may not be done that often. It's part of our test code which uses a wide variety tools: java, ant, make, and xml.
Our java testing tool parses XML. It gets an argument via an XML tag from a file:
<TAG>-Darg1="argument1" -Darg2="argument2"</TAG>
Within the java code, I'm calling the make command. The make command is invoked from java (via ProcessBuilder).
In the makefile, I am calling ant where the -Darg="argument1" -Darg2="argument2" args should be passed.
But it's not working.
Anyways, it seems that the -D from the -Darg1= ... part is not compatible with make, so I'm trying to enclose that in a variable that I can pass through make to ant. Within ant it's taking the variable "RULES_ARG" and treating that as one argument, instead of two.
I've tried various quoting mechanisms in xml: "-Darg1=argument1 -Darg2=argument2", "-Darg1="argument1" -Darg2="argument2""
and also where it's invoked in java: "RULES_ARGS="+RulesArgs+" ", "RULES_ARGS=\""+RulesArgs+"\" " (in combination with the xml part).
etc. all with no desirable result.
I was wondering if anyone has tried to do something similar, and the working approach to the problem?
Thanks in advance.
Are you saying you're trying to run make with -D options? That won't work because make doesn't support -D.
If you're trying to pass some flags through the make command line to be used on the ant invocation, then you should do something like this: when you run make set a variable on the make command line:
make ANTFLAGS='-DFOO -DBAR -DBAZ'
Then inside your makefile, when you run ant, pass that variable:
runant:
ant $(ANTFLAGS) ...
If that's not what you're trying to do please clarify your question. For example, you say it's not working and no desirable result, but you don't give any details about what errors or incorrect behavior you see. Such error messages would go a long way towards clarifying exactly what's going on.
Related
Follow these steps to reproduce:
Create a '.jsh' script with some kind of error(missing import, syntax error etc)
open a jshell
/open the erroneous script
The /open command will finish silently.
The elements declared in that script won't be in the current namespace.
Is there a way to force jshell to spit out the error and it's location in the script if an erroneous script is loaded? Setting the feedback level to verbose doesn't change anything.
jshell imports a lot of classes on startup and any subsequent snippet (even if load from file) can use them, SO few of the missing imports can be taken care here.
--no-startup can be used to disable this behavior.
This problem seems to be related to the „forward references“ feature of JShell: When a method or type declaration refers to a still undefined variable or method, JShell checks only for syntactical correctness and delays the compilation of the method resp. type. In interactive mode it prints a helpful message like „method x refers to undefined method y and can’t be invoked until method y is defined“. But while a startup script runs, that message is not printed and in many cases the script executes nothing, which is very confusing. I have not found a way to enable the printing of that message.
Hypercritical persons might say that this bug/feature renders JShell useless for „real“ scripting applications, and I tend to agree.
I propose that the „forward references“ feature be disabled during the execution of scripts. In order to make forward references still possible, the entire script should be considered ONE big snippet. That would also eliminate the problem with line-numbers-per-snippet which makes no sense at all with code parsed from scripts.
Short version
How do I configure the icedtea-plugin package for ubuntu precise in such a way that it executes all applets with a specific memory limit by default? Preferrably using command line tools and no graphical user interface.
Long version
I'm building a kiosk setup using java applets, and would like to increase the default memory limit for all these applets. The kiosk is based on ubuntu linux, using the icedtea-plugin package as a browser plugin. As building the kiosk is automated and headless, I'd rather configure it from the command line if possible.
Here is what I've found out so far:
I'll probably have to specify a -Xmx… command line argument which will be used when starting the java virtual machine for the plugin.
It is possible to pass such arguments using a parameter in the HTML code, i.e.
<param name="java_arguments" value="-Xmx…">.
But that would mean modifying all my HTML files, which I'd rather avoid. And there is a good chance that I'd have to sign my code to make this work, which adds quite a lot of problems in other places of my project.
On my desktop system, using the Oracle VM, there is an application ControlPanel which I can use to set these command line switches. It seems that the configuration gets written to a file ~/.java/deployment/deployment.properties, where it is associated with the key deployment.javaws.jre.0.args.
That key name is not described in the specs for that file. The number 0 seems to refer to one specific JVM, as represented in one row of the table in the control panel. For each JVM, there are several values besides this args value, all sharing a common index number.
According to this wiki section, IcedTea supports a similar file, located at ~/.icedtea/. Not sure whether that means ~/.icedtea/deployment.properties or rather ~/.icedtea/deployment/deployment.properties. IcedTea does not seem to come with a ControlPanel.
As you can see, there are many hints as to how this might work, but so far I haven't seen any definite answer as to what will work. There are still many open questions:
Do I have to create this icedtea deployment file?
Do I have to include a directory named deployment in the path?
Which is the minimal set of keys that I need to specify to make this file work as intended?
Is the args value even supported by OpenJDK, even if it is not described in the corresponding specification?
I guess I could try out all possible combinations to see whether one of them works, but doing so will take considerable time. So I'm asking here, to see if someone has experience in this, and can provide a quiecker answer. If not, I guess I'll answer my own question eventually, once I've tried things the hard way.
as oracle says: http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-Desktop/html/plugin.html.
following environment variable should be set to add additional arguments to applets:
_JPI_VM_OPTIONS=-Xmx…
Moreover as the reffered site says:
Please note that you need to restart your browser after you set the environment variable. If you are setting this environment variable in the command shell, then you must use the same command shell to launch the browser so that the browser inherits the value of environment variable.
However if you doubt how to set environment variable in ubuntu, following post might be helpful: Make $JAVA_HOME easily changable in Ubuntu
I just had a look at the source code of the icedtea-web plugin. The part of the code which builds the command line seems to have no provision at all for including custom arguments, no matter their origin. So it does not matter which config files, HTML files or environment variables I edit, the code there will build the same command line to call java, disregarding all my whishes.
So if all configuration fails, what remains is hackery. One approach would be to patch the source code of the plugin, and make it include additional words into its command line. For a static addition, i.e. a single size change, this would be fairly easy. A more general fix would include the content of some environment variable, the way Peter's answer suggests. That would mean word-splitting the environment variable in question, and allocating the space for the array of words dynamically. Quite a bit of work at the C level.
As an alternative, one could tackle this at the level of the executed binary. The plugin apparently has the path of the executable hard-coded into it. In my case, this path is /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java. One can rename that binary to java.orig, and put a shell script in its place, with the following content:
#!/bin/bash
for i in "$#"; do
if [[ "${i}" == sun.applet.PluginMain ]]; then
exec "$0.orig" -Xmx512m "$#"
fi
done
exec "$0.orig" "$#"
One consequence of this approach is the fact that ps will no longer print these applets as java but instead as java.orig. Should not be a problem in most cases.
I was hoping someone will be able to give me some advice. I am running a program in java and need to include a function that has already been written in bash. I honestly am not sure where to begin.
I have file that I process to some extent with my java program (I take out duplicates, sort names, etc). I end up with several paths to other files.
for(String go:parsun){
pathed="/home/S/Data/"+go+"/Ana/lysis/"+go+"/file.vcf";
System.out.println("Pathed: " + pathed);
}
I need to take these paths and put them through a bash program which takes the contents of the files and does some math-ish things to their innards.
I have seen around the internet that you could do something like this to run a bash script in java:
Process pro=Runtime.getRuntime().exec("shellfilepath");
BufferedReader read=new BufferedReader(new InputStreamReader(pro.getInputStream());
But I am not sure I understand how that works, or how I would go about passing my file paths to it. Also, another consideration is that I will have to slightly modify the bash script to allow it to take in these paths. Previously the paths were hardcoded before hand. I will also need some suggestions on that front.
Is this the best way to have bash and Java work together? Is there another technique? If not, could someone help me with understanding how to use Runtime?
Thanks
You could use:
Process process = Runtime.getRuntime().exec("bash_command " + pathed);
The pathed filename will be passed in as the $1 argument.
The following looks like a pretty good intro into running shell scripts from java:
http://obscuredclarity.blogspot.com/2011/07/executing-shell-script-from-java.html
It shows how to modify your shell script to accept arguments, and how to use Runtime.exec() to call the script and pass arguments to it.
Also see the API for the Runtime class: http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
Runtime.exec(String) is a good way to do this, but you need to be careful about whether or not the path to your script contains spaces. A better approach would be to use Runtime.exec(String[]). This will make sure that Java treats your script path as one command parameter instead of trying to split it on spaces.
Process process = Runtime.exec(new String[] {shellFilePath});
Also, make sure to include #!/bin/bash at the top of your script.
You have two choices:
Rewrite the logic from the bash script in Java. This will execute faster and be less brittle, but if the bash script is large and complex or legacy code that nobody left understands, it may be more work than you want to do.
Execute the bash script using ProcessBuilder; you can pass command-line arguments to it. This comes with its own set of issues having to do with coordinating between two processes. If the bash script does not expect any input things are a little simpler, but in general you need to start the script, run input and output handlers on separate threads, and then wait for termination. This is a little difficult to get right if you've never done it, although there are good examples on the web.
I need to diagnose all invoked methods in a class(either declared in the class or not) using it's source code. Means that give the class source code to a method as an input and get the invoked method by the class as the output. In fact I need a class/method which operates same as java lexical analyzer .
Is there any method to diagnose all invoked methods ?
of course I tried to use Runtime.traceMethodCalls(); to solve the problem but there was no output. I've read I need to run java debug with java -g but unfortunately when I try to run java -g it makes error. Now what should I do ? Is there any approach ?
1) In the general case, no. Reflection will always allow the code to make method calls that you won't be able to analyze without actually running the code.
2) Tracing the method calls won't give you the full picture either, since a method is not in any way guaranteed (or even likely) to make all the calls it can every time you call it.
Your best bet is some kind of "best effort" code analysis. You may want to try enlisting the compiler's help with that. For example, compile the code and analyze the generated class file for all emitted external symbols. It won't guarantee catching every call (see #1), but it will get you close in most cases.
You can utilize one of the open source static analyzers for Java as a starting point. Checkstyle allows you to build your own modules. Soot has a pretty flexible API and a good example of call analysis. FindBugs might also allow you too write a custom module. AFAIK all three are embeddable in the form of a JAR, so you can incorporate whatever you come up with into your own custom program.
From your question it is hard to determine what is exactly problem you're trying to solve.
But in case:
If you want to analyze source code, to see which parts of it are redundant and may be removed, then you could use some IDE (Eclipse, IntelliJ IDEA Community Edition etc.) In IDE's you have features to search for usages of method and also you have functionality to analyze code and highlight unused methods as warnings/errors.
If you want to see where during runtime some method is called, then you could use profiling tool to collect information on those method invocations. Depending on tool you could see also from where those methods were called. But bare in mind, that when you execute program, then it is not guaranteed that your interesting method is called from every possible place.
if you are developing an automated tool for displaying calling graphs of methods. Then you need to parse source and start working with code entities. One way would be to implement your own compiler and go on from there. But easier way would be to reuse opensourced parser/compiler/analyzer and build your tool around it.
I've used IntelliJ IDEA CE that has such functionalitys and may be downloaded with source http://www.jetbrains.org/display/IJOS/Home
Also there is well known product Eclipse that has its sources available.
Both of these products have enormous code base, so isolating interesting part would be difficult. But it would still be easier than writing your own java compiler and werifying that it works for every corner case.
For analyzing the bytecode as mentioned above you could take a look at JBoss Bytecode. It is more for testing but may also be helpful for analyzing code.
sven.malvik.de
You may plug into the compiler.
Have a look the source of Project Lombok for instance.
There is no general mechanism, so they have one mechanism for javac and one for eclipse's compiler.
http://projectlombok.org/
I am looking for a way to do a test method in TestNG such that when I click in a certain file in Eclipse and make Run -> TestNG, it will pass that selected file's full name and the test receives that same file name as argument.
Is this possible to do with TestNG? I know how to make Eclipse send the correct argument, I am not just sure how to make TestNG accept it.
Thanks
This is kind of hacky... but you could try instantiating it from Ant. Rather than write a build.xml just call the testNG task directly and feed it the class/file name. That way all the work is done for you. Also I believe that TestNG requires it's own JVM when it's running, so this will take care of that as well.
Edit: I think I misread your question.