To make my question abit more specific I'm wondering if a compiled java program can import methods from a simple "text.txt" file, basiclly from text characters?? Is this possible? If so how?
Yes it is possible, here is an example of how to do it: example. On that page a string is compiled but it is the same basic principle. If you read the stuff in your text file into a string you can do the same thing.
This can be done easily using BeanShell.
http://www.beanshell.org/
Been around for years, rock solid, works.
It can, but if you want those methods to be written in standard Java then it will require a bit of technical creativity.
Essentially, you can use the "Scripting API" ( http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html ). This API allows you to execute "scripts" in any language as part of your greater Java application. To get it to run Java, you'd need to create a ScriptEngine implementation which could take the source, run it through the compiler API ( http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html ), and execute it.
If the method doesn't have to be coded in Java, then you can use the scripting API pretty much out-of-the-box, along with one of the standard scripting engines. (The JavaScript engine is very well tested, for example).
All methods must be part of a class.
You can only use methods in bytecode.
IFF your text file defines an unique class, you can use the Java Compiler API and reflection to use such a method.
Related
I'm just starting to learn and the books say that I can use "pre-coded" classes and objects. How do I call these classes/objects in the beginning of my program? I understand basically what they are, and the idea that I can use these classes/objects in place of writing fresh code every time, but I cannot seem to figure out where I find these things and how I implement them.
You certainly talk about the Java classes that come in JRE/JDK.
Those are used by including the jar in your classpath and provides the "default" java classes.
Like String in java.util package.
If you want to look at them, in the JDK you'll find the sources of these class.
"Pre-coded", or pre-written Java classes, are pretty much the same concept as the Java API - someone has written the code for you, was kind enough to document how you can use the code, and you may create instances (as necessary) through the prescribed way.
Say, for instance, I want an ArrayList holding Strings. I would then code ArrayList<String> words = new ArrayList<String>(). You wouldn't have to go through the process of writing a dynamic self-expanding vector.
Could someone please advise on how to get Disk_Geometry in JNA.
I know this is straight forward in C++ by creating a handle for a disk using CreateFile(), using Deviceiocontrol to query it and using DISK_GEOMETRY to get different disk attributes. I would like to be able to do the same thing in Java using JNA, but DISK_GEOMETRY type is missing in the Kernel32.
Please help.
You can easily extend the interface definitions provided with JNA to add anything that's "missing". You can add any function, structure or constant definitions that suit your purpose.
public interface MyKernel32 extends Kernel32 {
public class DISK_GEOMETRY extends Structure {
// Fill in specifics of the structure here, following the type mapping rules
// in the JNA documentation, or use [JNAerator][1] to auto-generate the mapping.
}
}
I do not know how to do this with JNA but would like to suggest you to use WMI instead. The WMI class [Win32_DiskDrive][1] seems to be a good candidate for you. It contains what you need and probably even more. Now the question is "how to call WMI from java?" There are number of ways. The simplest one is to write script using JScript or VBS and call it from java using ProcessBuilder. This way is simple and does not require dealing with native code and external libraries but could be a little bit slow because process-to-process communication is used.
Other way is to use one of available java-to-com packages. For example JaWin, JInterop, JIntegra.
I have a java program that processes information, but I want to make it so the end user can write javascripts that dictate what to do with this info. Like this
//Java
private void newData(int var1) {
script.newData(var1);
}
and then
//Javascript
function newData(var var1) {
someVar = var1;
processVar();
}
I have looked into something called rhino, but I really am having trouble understanding the concept of rhino. Anyone know what to do?
You want Rhino. Rhino is a javascript runtime implemented in Java. It is suitable for embedding in Java applications.
What you want to do is create your Java classes and objects and then make them accessible to a Javascript environment. Fortunately this is very easy with Rhino. Read this tutorial and pay close attention to the first and second sections (RunScript: A simple embedding and Expose Java APIs).
The first section is about executing Javascript within a Java application. You will need to adapt their sample code a little to provide some way for the end user to hand javascript code to you (in a file or stream) for you to execute. It won't be difficult.
The second section is about making your Java stuff available to the Javascript stuff. In the simplest case you don't need to do anything--all of Java is available to Rhino javascript automatically. But you can very easily pretty up the interface and provide something easier for the end user to use if you want.
With Rhino you can also go in the other direction--you can make Javascript objects available to the Java environment. This is a little more complicated, but is covered in the rest of the tutorial. You may not need to do this either.
Good day all,
I am trying to figure out how to allow users to call a method on some specified data.
I would like to provide a predefined set of functions:
Moving average, moving_ave(x,5) ..would be a 5 day moving average on x.
3*x+y....and so on...
So basically, i will provide the users with various data series (x,y,z....) and a set of functions moving_ave, + - / * ....and they should be able to write simple formulas (restricted to the functions i provide).
how can this be done?
I will be deploying this on App Engine for Java.
so for i have found out about JSR-223...but i'm not sure if its appropriate? I am thinking i can use the Eval function.
Thanks,
It sounds like what you want is an interpreter for a simple grammar. Be very wary of approaches such as that suggested by Aerosteak; allowing your user to call functions in your code directly is dangerous, and it's easy to make mistakes sandboxing it, resulting in security vulnerabilities. It'll also require you to write your own parser.
The easiest approach is probably to use an existing language - Javascript probably fits very well, and you can use Rhino, a Javascript interpreter written in Java.
You will need to use Reflection to call unknow Method. Look a Apache BeanUtil.
You can have a TextBox with the value: 1,2,3, Convert these values to Object Array.
Have another ComBo Box with all you possible Method you can call.
Then use Bean Util to call the method with the Object Array.
For Exemple:
class MyMathManager{
public void doCalculationType1(Object args...){..}
public void doCalculationType2(Object args...){..}
public void doCalculationType3(Object args...){..}
Then Look at the Java of BeanUtil to call these Method.
Good Luck.
This sounds like something that could probably be done on the client, rather than the server. You could write a few handy javascript functions that call a restful API on the server to provide the needed data to a few more handy javascript functions that do the useful calculations. It's almost always safe to allow users to eval on their own clients.. they can do it in any case.
It seems that Java is not set up to do what I have previously done in C++ (no big surprise there). I've got a set of rules that are generated from another application (a series of if-then checks). These rules change from time to time, so in C++ I would do this:
double variableForRules=1;
bool condition=false;
#include "rules.out";
if(condition) //do something
Essentially the if-then checks in rules.out would use the "variableForRules" (and several other variables) to decide whether condition should be set to true. If it gets set to true after the eval of the rules, the program does something.
Is there a similar way to do this in Java? Or is my only option to have rules.out actually be an entire class that needs to be instantiated, etc.?
Thanks!
Since you're autogenerating that rules.out, you could autogenerate your Java function as well. Hopefully it's not too painful to add that functionality.
Since there is no preprocessor in Java, you can't do this. Like you said, you have to implement your logic inside a class.
Maybe you could use scripting for that. You could take a look at the Java Scripting Programmer's guide.
In Java, it would be common for the other application to save the rules into an .xml or .properties file, and then have Java read in that file.
rules.out actually has to be an entire class that needs to be instantiated for code to be executed.
Since rules.out is generated by third-party application, best thing would be to write your own CppToJavaTransformer that reads file rules.out as input and generates Rules.java. This assumes rules.out is available before compile time and Rules.java will be used at compile time. Drawback of this is that there is an extra transformation required.
Alternately you can write code that interprets rules.out and execute required instructions using introspection. This is hard way but rules.out can be changed at runtime as well.
Even if could include the rules file it would be of no help. Your rule are dynamic as you said. Looks like you java code needs to change for a different scenario.
You could try using reflection (See: Creating New Objects and Invoking Methods by Name)
First generate a Rules.java in the manner in which you currently build rules.out and compile it.
Then load the class file into your app at runtime in the manner in which JDBC drivers were traditionally loaded.
Class clazz = Class.forName("com.mydomain.Rules");
If your app runs for long periods of time (longer then the lifetime of a single Rules.class file) then you would have to create your own ClassLoader in order to swap out the underlying class during a single runtime.