Make an extendable java program - java

I'm making a java command prompt (like the Windows CMD).
But I want to make it possible for other developers to add some commands to my program by putting their own *.jar files in a folder called "extensions".
I know this is possible because Minecraft can be modded with MC Forge like that (by putting your mod jars in a folder called "mods")
Example:
public class myExtension extends Extension {
//A method called by my program
#Override
public void onLoad() {
//Register a command in the main class...
Command cmd = Commands.registerCommand("hello");
//Add an event listener...
cmd.addEventListener(new CommandEventListener() {
public void onCommandExecute() {
//Print "Hello World !" to console when the command is executed...
Console.getConsole().print("Hello World !");
}
});
}
}
So now the guy who made this should be able to export this program as a jar file and put it in the "extensions" folder. But how can I make that my program register this jar as an extension ?
I you know the answer the please write it down ! Thank you !

Related

Is there a way to run a -.jar file (created with javaFx) without displaying anything(GUI, progressbar, ...)?

I wrote a java file including javaFX. Now, I want to run this file, like
java -jar example.jar
But I'd like to suppress the graphical output.
Is there any possible, like a flag or anything else, to do this?
My program normally shows a progressbar and after that a video of the simulation.
Thanks a lot.
To elaborate on JB Nizet's comment.
JAR files have manifests. In order to run your JAR using the command java -jar example.jar, the manifest must have a Main-Class entry. And your main class must have a main() method.
So launch your app like so...
java -jar example.jar NO_GUI
And in your main() method, write something like the following...
public static void main(String[] args) {
if (args.length > 0 && "NO_GUI".equals(args[0]) {
// Don't show GUI
}
else {
// Show the GUI.
}
}
You could call hide() on the scene so the window dissapears.
There is not really a way to force this.
Instead, implement it as feature. Create a command line argument nogui and react to it:
public static void main(String[] args) {
boolean useGui = true;
if (args.length > 0 && args[0].equals("nogui")) {
doNotUseGui = false;
}
// Create your program and give it the flag
Program prog = new Program(useGui);
...
}
Note that theoretically it would be possible to hack your application and remove any such calls, or to suppress the calls on a native level. But I guess that is not really the route you want to go.

Specify Java command line options after jar execution

In an effort to make my app more OSX friendly, I am trying to set the dock name of my program to something like MyApp instead of a fully qualified class name (the default), such as myproject.mypackage.mysubpackage.myclass. Obviously, the first is much more appealing.
To do this, I use the OSX -Xdock:name command as a command line option when executing my .jar file. So to execute it, the command might look something like java -Xdock:name=MyApp -jar /mypath/myjar.jar. This works perfectly and sets the .jar's dock name to MyApp. But the issue is that this .jar will never be executed via command line and will be a double-clicked runnable .jar with a GUI display.
The only way I have thought of to set this command line option programmatically is to have a second class execute the class that actually starts the program. So something like this:
public class AppStarter {
public static void main(String[] args) {
String cmd = "java -Xdock:name=MyApp -cp myproject/mypackage/AppBuilder";
try {
Runtime runtime = Runtime.getRuntime();
runtime.exec(cmd);
} catch(IOException ex) {
//Display error message
}
}
}
public class AppBuilder {
public static void main(String[] args) {
//Start actual program and build GUI display
}
}
So here, AppStarter sets the command line options for AppBuilder, which when executed, has the dock name MyApp. The problem I see with this is that it is very tightly coupled. If for some reason the command line is inaccessible on the device or some IOException keeps getting thrown, literally nothing will happen with the program and it will be dead. There would be no way for the average computer user to recover from this.
So I'm wondering if it is possible to perhaps set these command line options after the .jar has already started executing. The old way to programmatically set the app's name has been ineffective for several OSX updates, so I'm stuck with only this command line option. Thanks for any advice.
Once the java command is executed, the command line arguments are parsed and set for the running JVM. You cannot change it any more.
This is usually handled by execution scripts (bash, etc.). If you cannot use them, you can use your approach, but the biggest disadvantage is that it will be running in a separate process.

Update java desktop app

I am trying to develop a module that can update my running Java Desktop App.
The problem is that I have to replace the actual running jar with another jar, all the while displaying an image and a progress bar with the remaining time of the update process.
One solution I thought about is that I can put a jar in my main jar, and when launching the update process, to extract that second jar which will display the image and the progess bar, and also which will replace the old main jar with a new main jar.
My question is if this is possible and how can I do it.
I do not have a lot of experience with java and java packaging so if you have any examples or links, it would be of great help for me.
Thank you very much.
R.
Run this code when press UPDATE button ..
if(Desktop.isDesktopSupported()){
try {
Desktop.getDesktop().open(new File("update.jar"));
System.exit(0);
} catch (IOException ex) {
}
}
This will open update.jar and close main.jar. Now run this code from main class of update.jar
//wait sometime for terminate main.jar
try{
Thread.sleep(5000);
} catch(Exception e){
e.printStackTrace();
}
if(isUpdateVersionAvailable()) { //first check update from database
if(copyMainJarFileFromServer()){ //copy newMain.jar from server and paste
new File("main.jar").delete(); //delete main.jar
rename(new File("newMain.jar")); //rename newMain.jar to main.jar
}
}
boolean isUpdateVersionAvailable() {
//todo
}
boolean copyMainJarFileFromServer() {
//todo
}
void rename(File file){
file.renameTo(new File("main.jar"));
}
You can have a starter jar that checks for updates and launches the app from the main jar.
It will show start logo, an image, that standard java can display at start-up.
The start0er could also be used to restart the app in another interface language.
package starter;
...
public class StarterApp {
public static void main(String[] args) {
String workDir = System.getProperty("user.dir");
Path mainJar = Paths.get(workDir + "...");
Path nextMainJar = Paths.get(workDir + "...");
if (Files.exists(nextMainJar)) {
Files.copy(nextMainJar, mainJar, StandardCopyAction.REPLACE_EXISTING);
}
URLClassLoader classLoader = new URLClassLoader(new URL[] {mainJar.toURL()});
Class<?> appClass = classLoader.find("mainjar.MainApp");
... instantiate the app
}
As you see the main jar must not be loaded from too early, maybe not be on the class path entirely, and hence the use of a separate ClassLoader. The same might probably be done with the main jar on the class path of the starter app, and using Class.forName("mainjar.MainApp"). The Class-Path can be specified in META-INF/MANIFEST.MF.
The secundary jars may reside in a lib/ directory.
For those readers wanting more modular, service oriented, updateable apps, one could make an OSGi application, a container for bundles (=jars), that provide exchangable services and life-time control.

How to open your eclipse project in a window?

How do I export my Eclipse project so that it is its own console application sort of thing.
Every time I try to run the .jar file after exporting it, a window pops up saying that it couldn't open the program. Is there some code I need to enter in order to make it its own window, like when you run it in eclipse, except it is its own window and application. Here is an example of how I have coded it:
public class main {
public static void main(String[] args) {
Scanner text = new Scanner(System. in );
System.out.println("Ready:");
boolean loop2 = true;
while (loop2 = true) {
String text1 = text.nextLine();
switch (text1) {
case "hi":
System.out.println("Greetings!");
break;
}
}
}
}
Any help would be nice! I am trying to make this for sending to my friends, too, just so you know.
In the navigator view, right-click on your project > Export > Java > Jar. Be sure to indicate your class as main class.
Then, once saved, you can run the jar by double clicking on it, or using java -jar jarfile.jar from command line.

class file for javax.serverlet.GenericServlet not found

I have stuck in the class->header file for couple days!
I have tried on jni on Client by http://netbeans.org/kb/docs/cnd/beginning-jni-linux.html and http://ringlord.com/jni-howto.html. And it succeeded in return "hello JNI C++" from JNI's (.cpp)file. Here are my steps:
create native function and in client.java
clean &build this client.java on Netbeans IDE, then result a client.class file
javah –jni [package].[classname]
create a dynamic library C++ project as first reference does, and put client.h into source file, and put some hello code into (.cpp)file ---> It works!
However, I tried to do the same thing on the servlet side and it's not working
Servlet.java->Servlet.class : ok!
Servlet.class->Servlet.h: fail!!!!
Error : cannot access javax.servlet.GenericServlet
class file for javax.servlet.GenericServlet not found
The following are solutions I have found and tried so far,
check the package name
sudo gedit /etc/profile,sudo gedit .bashrc, sudo /etc/environment; add JAVA_HOME & CLASSPATH on them, and source them to update, then echo $JAVA_HOME, echo $CLASSPATH to verify
download servlet-api-6.0.14.jar & servlet-api-5.0.16.jar from http://www.jarfinder.com/index.php/java/info/javax.servlet.GenericServlet
,and add above two (.jar) by netbeans IDE->server->property->libraries->Add JAR
Please tell me how to figure it out this issue, thank you very much!!Btw, I am using hessianServlet
NativeWrapper.java (you run javah only on this class)
class NativeWrapper {
// either
static {
System.loadLibrary("MyOpenCVNative");
}
// or
public NativeWrapper() {
System.loadLibrary("MyOpenCVNative");
}
public native void callNative();
}
MyServlet.java
class MyServlet extends javax.servlet.GenericServlet {
private NativeWrapper nativeWrapper = new NativeWrapper();
public void someServletMethod() {
nativeWrapper.callNative();
}
}

Categories