I want to launch static function from jar file and recieve its return value during install time. Is there some other way, rather then executing java.exe?
I really fail to understand the reason for downvotes...
You can run custom code during the installation by extending from CustomCodeAction of InstallAnywhere. All you need to do is to override the install and uninstall methods of the base class. Please find the snippet of the sample code below.
public class MyCustomCodeAction extends CustomCodeAction {
public void install(InstallerProxy proxy) throws InstallException {
// call the static function of your jar here
}
public void uninstall(UninstallerProxy Uproxy) throws InstallException {
// you can do something here if you need (not must)
}
}
Good luck!
Related
Currently trying to use Junit to create some concurrency tests to run in Jmeter
My current project Structure looks like this
From RasterTest.java, I'm trying to call a method in CommonMethods.java and a method in SetUp.java.
When I run this as a JUnit Test in eclipse, the methods in CommonMethods and SetUp are called and everything works fine. When I export this to a JAR and run the same in Jmeter, I get an error given below (getProperty is a method inside SetUp )
Trace -- java.lang.NoSuchMethodError: utils.SetUp.getProperty
I couldn't find any resources, hence i thought this maybe due to the methods being static, but i tried accessing the method by creating an object too. That Didn't help, got the same error.
Attaching the class CommonMethods for reference :
public class CommonMethods {
public String getProperty (String key) {
// Some code
}
public void setUrl() {
System.out.println("Hello world");
}
}
Calling it as :
CommonMethods cm = new CommonMethods();
System.out.println(cm.getProperty("URL"));
cm.setUrl();
I don't understand why Jmeter throws this error and how do i get around it? Thanks!
Hope you are using jpgc-perfmon-2.1. If not zip that into jmeter and then remove plugin cmn jar file from lib and update jmeter-plugins-cmn-jmeter.jar file with the latest version.
Seems like its kind of configuration issue.
I am building my Spring Boot 1.5 + Kotlin 1.2.41 project into a jar. One of the interfaces in the jar has the #JvmDefault and it compiles fine with the flag (if I remove the flag, it fails).
Now, I am trying to use this interface in another java project, in which I define the Kotlin project as a dependency.
In one implementing class, I don't override the default method. Intellij seems to be OK with it, as it doesn't complain. However, when I compile with Maven, I get :
[ERROR] attempting to assign weaker access privileges; was public
If I implement the method (with some dummy implementation), then it compiles... but it defeats the purpose of the default interface.
Any idea what could be wrong ?
When opening the Kotlin interface code from the java project, here's the decompiled code I see :
public interface CrawlerOutput {
#kotlin.jvm.JvmDefault public open fun finalize(): kotlin.Unit { /* compiled code */ }
public abstract fun output(analyzedRepository: com.myCompany.Repository): kotlin.Unit
}
My java code implementing the interface :
public class CsvOutput implements CrawlerOutput {
#Override
public void output(Repository repository) throws IOException {
log.info("own output is receiving some data !");
}
/**
* IF I REMOVE BELOW METHOD, MAVEN CAN'T COMPILE IT ANYMORE,
* COMPLAINING OF WEAKER ACCESS PRIVILEGE
*/
#Override
public void finalize(){
}
}
Am I missing something ?
Thanks
Vincent
Your method name conflicts with java.lang.Object.finalize(). The error should be fixed if you choose a different method name.
Android Studio and JVM always update its versions. As a result of that some of you may experience this error message.
Inheritance from an interface with '#JvmDefault' members is only allowed with -Xjvm-default option
Don't worry . The solution is very simple. Just add below code part to the end of android block of your app level build.gradle file and sync.
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
freeCompilerArgs += [
"-Xjvm-default=all",
]
}
}
For performance reasons, I have a class that stores a Map whose key is a Class<?> and its value is function of that class's fields. The map is populated during code execution according to the type of the calling object. The above is a generalization/simplification
public class Cache {
private static final Map<Class<?>, String> fieldsList = ...;
//Synchronization omitted for brevity
public String getHqlFor(Class<?> entity){
if (!fieldsList.containsKey(entity))
fieldsList.put(entity,createHql(entity));
return fieldsList.get(entity);
}
}
During development, thanks to the help of Jrebel, I often make modifications to classes by changing entire properties or just their names. I can continue development just fine. However, if I already put a value into the cache it will be stale forever.
What I am asking here is if it is possible to intercept the event that a class in the classpath has changed. Very broad... But my specific problem is very simple: since I have such a need only during development, I just want to wipe that cache in case any class in my classpath changes.
How can I accomplish this? I don't need to do anything special than intercepting the event and simply wiping the cache
JRebel has a plugin API that you can use to trigger code on class reloads. The tutorial complete with example application and plugin available here: https://manuals.zeroturnaround.com/jrebel/advanced/custom.html
The JRebel plugin is a self-contained jar built against the JRebel SDK, which is attached to the running application via the JVM argument -Drebel.plugins=/path/to/my-plugin.jar. The JRebel agent attached to the application will load and start plugins from this argument.
If the application is not started with the JRebel agent, the plugin is simply not loaded.
In your example you want to register a ClassEventListener that will clear the Cache.fieldsList map. As it is a private field, you need to access it via reflection or add a get/clear method via a ClassBytecodeProcessor
public class MyPlugin implements Plugin {
void preinit() {
ReloaderFactory.getInstance().addClassReloadListener(new ClassEventListenerAdapter(0) {
#Override
public void onClassEvent(int eventType, Class<?> klass) throws Exception {
Cache.clear();
}
});
}
// ... other methods ...
}
And to clear the map
public class CacheCBP extends JavassistClassBytecodeProcessor {
public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) {
ctClass.addMethod(CtMethod.make("public static void clear() { fieldsList.clear(); }", ctClass));
}
}
However a better option is to only clear/recalculate the single class entry on class reload if possible. The example didn't display whether the info computed from one class depended on superclass infos, but if this is true, the JRebel SDK has methods to register a reload listener on the class hierarchy as well.
There is an existing class ClassValue which already does the job for you:
public class Cache {
private final ClassValue<String> backend = new ClassValue<String>() {
#Override
protected String computeValue(Class<?> entity) {
return createHql(entity);
}
};
public String getHqlFor(Class<?> entity){
return backend.get(entity);
}
}
When you call get, it will call computeValue if this is the first call for this specific Class argument or return the already existing value otherwise. It does already care thread safety and for allowing classes to get garbage collected. You don’t need to know when class unloading actually happens.
I am new to Android . . .Till now i was thinking that all the resource's ids are in android app are managed and mapped using R.java file. But i got an application in market to work on,in which i did not found R.java/R.class file in it, after decompiling it using dex2jar utility
my issue is that till now my work was dependant on R.java/ R.class file , I was accessing all ids in an app using this file and reflection concept. But since this app does not contain R.class file my work is stuckked for now.
Interesting thing for me is that, when i create dex file from this jar and replace it in app and sign app using one_click_signer ,the app works fine on mobile.
So ,
i want to know how they could have managed ids without R.java?
Is it possible to have an application without R.java, or i may have
made mistake while decompiling app?
how can i find ids in the application?
They used R but it was optimized away.
Let's say the app had this code:
public class R {
public static class id {
public static final int something = 123456;
}
}
public class Main {
public void doSomething() {
doSomethingWith(R.id.something);
}
}
When compiling Java, the compiler "inlines" static final fields - replacing the field access with the value, since the compiler already knows the value. That means the compiler translates the code to this:
public class R {
public static class id {
public static final int something = 123456;
}
}
public class Main {
public void doSomething() {
doSomethingWith(123456);
}
}
If the application is optimized/obfuscated with Proguard - which is common with Android applications - then Proguard would then detect that the class "R" is not used and delete it, resulting in this:
public class Main {
public void doSomething() {
doSomethingWith(123456);
}
}
I am trying to accessing dll methods in java which has been written in c#. From the following code i am trying to build dll which is generated successfully.
using System;
using Microsoft.Win32;
namespace CyberoamWinHelper
{
public class RegistryAccess
{
public static String getValue(String key)
{
RegistryKey rk = Registry.CurrentUser;
RegistryKey rk1=rk.OpenSubKey("Software\\Test", RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl);
rk1.SetValue(key, "val1");
return rk1.GetValue(key).ToString();
}
public static void createSubkey(String name)
{
RegistryKey rk = Registry.CurrentUser;
rk.CreateSubKey("Software\\Test");
}
}
}
After this i am loading the generated dll in my java program code of which is as follows
public class JNI {
/**
* #param args the command line arguments
*/
public native String getValue(String key);
public static void main(String[] args) {
// TODO code application logic here
try
{
System.loadLibrary("CyberoamWinHelper");
JNI j=new JNI();
System.out.println(j.getValue("abc"));
}
catch(UnsatisfiedLinkError e)
{
System.out.println("Ex" + e.getMessage());
}
}
}
After running this code it is giving me the following error.
"Exjni.JNI.getValue(Ljava/lang/String;)Ljava/lang/String;"
Well i am not understanding what this error is saying but i want to solve it.
And one more question i am having is since the method i am calling is a static method will it be called in this way? i mean to call static method we need
"classname.methodname"
so here will it be able to call the method?
You can only call methods via JNI if those methods were in fact designed to be called this way. Your methods absolutely are not. What you're doing here has (sorry to be so blunt) absolutely no chance of ever succeeding -- it simply doesn't work this way.
There are several ways you might proceed. One would be to learn about JNI and how to write libraries that actually work with it. Here is the canonical reference for this. Doing this with C# adds yet another layer of complexity, though.
Another way would be to give up on JNI altogether and use a more appropriate mechanism to access the methods. You can learn about JNA here; it would be entirely better suited to your goals.
Try jni4net. From their web site. Some detailed explanation is here -> How calling from Java to .NET works in jni4net
Your use of JNI is incorrect. It's difficult (although not impossible) to use JNI with C# libraries. There is an excellent tutorial on how to go about it here. Doing a C# JNI search on google shall reveal more.
You should also investigate something like Grasshopper..
EDIT
http://caffeine.berlios.de/site/documentation/quickstart.html
is a cool solution.
Helpful site for you: http://www.sahirshah.com/java/jni.html
Try:
public class myJNI {
/**
* #param args the command line arguments
*/
public static native String getValue(String key);
static
{
System.loadLibrary("CyberoamWinHelper");
}
public static void main(String[] args) {
// TODO code application logic here
try
{
String myKey = "abc";
System.out.println(getValue(myKey));
}
catch(UnsatisfiedLinkError e)
{
System.out.println("Ex" + e.getMessage());
}
}
}
You need to wrap the dll in a c++ dll as described in the above link. Just generate a header file with the "javah -jni myJNI" command and build a c++ dll with the function signature found in that header file.
Have a look at : http://www.codeproject.com/KB/cross-platform/javacsharp.aspx for a specific "hello world" example in C#