Loading DLL's on Java start up - java

I was wondering if it is possible to load a DLL as you execute a java application. Maybe through some command line parameters or something like that. Basically, I just want to make sure that when Java is started, my DLL is loaded so it doesn't have to be done only when I use it.

You have System.load(String filename) and System.loadLibrary(String libname).

It can be one in static initializatiion block
public class App {
static {
System.loadLibrary("libname");
}
public static void main(String[] args) throws Exception {
...

Related

How to launch program in psvm?

How to launch a program in psvm with one command?
How does the application know which class to launch first?
I know that psvm should only have starting command and nothing more.
Could you explain this to me?
I mean how to create proper public static void main(String[] args) in a simple program on Maven. Should I create a class i.e. Starter with method run (with sequence actions) and in psvm write new Starter().run()?
psvm stands for public static void main as shown below:
public static void main(String[] args) {
// Your code here
}
psvm is not a standard Java terminology. You can call it as a Java slang. It is the entry point in your standalone Java application i.e. when you run an executable jar, it will execute the class having psvm. There are so much of content about it on the internet e.g. https://dzone.com/articles/executable-java-applications
The main() is the first entry point of Java application. Java Virtual Machine is told to run an application by specifying its class using the application launcher & it will look for the main() with exact syntax of public static void main(String[]).
Considering your comments you want to do something like this :
public class Starter{
public static void main(String args) {
new Starter().run();
}
public void run() {
//your logic
}
}
once you write this,
you have multiple options to run this I am mentioning a few
1) by building jar and then executing that jar using java -jar command
2) or by executing maven command once you have compiled your program using mvn compile, mvn exec:java -Dexec.mainClass="complete name of your main class i.e including package name."
a few links
http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/
https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Maven_SE/Maven.html
hope this might help

Java application start point [duplicate]

This question already has answers here:
Entry point for Java applications: main(), init(), or run()?
(5 answers)
Closed 4 years ago.
I downloaded a simple java app and I want to know which file I need to run to start. There are several classes in the project and I do not know which one is the main one. Thank you.
Search for the class with the main method, that is the method where program execution starts. It looks like
public static void main(String[] args) { ... }
It's inside the class Main.java of your project:
public class Main extends Application {
public static void main(final String[] args) {
launch(args);
}
// [...]
}
Open the project in an IDE and run this class. Or do it manually in the console like:
javac Main.java
java Main
Only .jar files can automatically be started like .exe files. Your project only contains code and you will need to compile and run it on your own.
You should have public static void method called "main" taking array of String as parameter in one of the classes. Something like below
public static void main(String... args) {
System.out.println("test");
}
This is your entry point.
#Edit
OP added code so here is an answer update:
Your entry point is in class Main and method is
public static void main(final String[] args
If you're using intellij just run click on this method and select Run "Main.main()"

Eclipse run configuration

When i'm trying to run in Eclipse program which has no main method as Java Application it always requires a main method to define. Could anybody please explain is there any simpler way to make configuration run without implementing
public static void main(String[] args) {
}
Every program requires a main method.
The public static void main(String args[]) starts the main thread which your program will run on.
There's more information available here.
You need a main method to run your program.
public static void main(String[] args){
YourClass newObject = new YourClass();
}
Put this method in your class (replace YourClass with your existing class) and hit the run button.
run in Eclipse program which has no main method
NO.You won't be able to run a class by using IDE like Eclipse or anything else. Even you won't be able to run your java program by using Java command from your command prompt, until your java program has public static void main(String args[]) method on it. If you try to run, you will get below mentioned error
Error: Main method not found in class Test, please define the main
method as: public static void main(String[] args)
But you should also know that, we can run a Java program without main method on managed environment like the case of Applet, Servlet and MIDlet. There are actually different types of execution model available in Java, for example Applets which run on browser doesn't have main method. Servlet is also java program, which runs in a Servlet container and since Servlet is also a Java program, we can say that it runs without main method. Third one on this category is MIDlet, which runs on mobile devices.

Run Java with multiple classes on Netbeans

is there any good tutorial where I can learn how to run a Java Application with multiple classes (one class after the other, in a package) on Netbeans IDE 7.4?
Thank you!
I don't think that's possible, and I don't even know why you'd have multiple mains, but one way to do this manually is simply to write another main instead and call all the classes you want to run.
public class MultiMain {
public static void main(String[] args) throws IOException {
System.out.println(InsertionSort.class);
InsertionSort.main(args);
System.out.println(ConsoleTest.class);
ConsoleTest.main(args);
System.out.println(Main.class);
Main.main(args);
}
}
Also, maybe something like unit tests are what you are looking for ...?

Dynamic Link Library & Java

I made a dll in C++ and wrote this class in java:
public class VolumeControl {
public native float GetVolume();
public native void SetVolume(float val);
public native void VolumeUp();
public native void VolumeDown();
public native void Mute();
static {
System.load("some_path/VolumeControl.dll");
}
}
it works good, if I call functions from this file, but when I'm trying to do this:
public class Server {
public static void main(String[] args) {
VolumeControl ctrl = new VolumeControl();
ctrl.Mute();
}
}
I get this:
Exception in thread "main" java.lang.UnsatisfiedLinkError:
RemoteControl.VolumeControl.Mute()V
Both classes are in the same package, of course. How can I solve it? Thanks.
Update1: OK the problem was, that I added these classes to package. When I move them to default package, everything works good. But now if I want to use this dll with different packages, I need to rebuild it.
Update2: Actually I can't add it to package at all, when I'm trying: #javah VolumeControl, I get Error:
Could not find class file for 'VolumeControl'.
Update3: I added manually name of package to C++ functions and it works. Thanks.
The use of javah utility may help if integrated into a makefile to ensure the interface is always assumed on both side (client/server).

Categories

Resources