Linux but not windows? Java swing - java

So I wrote this program in Linux and it runs perfectly when I create a jar and run it.
But for some reason when I put this jar in windows it doesnt.
Some of it works but when I try to do a simple click action event it crashes and tells me
there is an
Exception in thread "AWT-EVentQueue-0" java.lang.NoClassDefFoundError: miginfocom/layout/cc
Well anyway I got rid of certain components of it and it works but with them it doesnt.
// MigLayout layout = new MigLayout("fillx");
JPanel content = new JPanel(/*layout*/);
/*CC componentConstraints = new CC();
CC c = new CC();
componentConstraints.alignX("center").spanX();
c.alignX("center").span();
c.alignY("bottom").span();*/
content.add(label/*, componentConstraints*/);
content.add(label1/*,c*/);
// after this I add it to a Jframe etc
Everything there that is commented out is the reason it wouldnt work on windows.
But it would on Linux, I have no idea why this is...
Can anyone tell why this is or where to find out. It would be much appreciated and useful for the future :)

I assume your classpath is not correct.
So the required libs (e.g MigLayout) cannot be found.
Windows uses different Path-Seperator (in classpath) than Linux.

You would have to create an "uberjar" if you expect to distribute just one JAR file and have everything working. On your dev machine you probably have access to your JAR's dependencies, but not so when you copy just the application JAR to Windows.

Related

Controlsfx Notifications don't work in compiled jar

I'm working on some app with GUI using javafx and have one trouble.
Notifications from controlsfx library (org.controlsfx.control.Notifications) work well when I'm running Main class from ItelliJ IDEA, but don't work in compiled jar (I have compiled jar using 'Build artifacts'). Other functionality work as well.
Here is method from Controll class that should open pop-up window
public void zAxisMouseEntered(MouseEvent mouseEvent) {
Notifications notif1 = Notifications.create();
notif1.title("Warning message");
notif1.text("This configuration is slower than 'SPL on Z axis'");
notif1.hideAfter(Duration.seconds(3));
notif1.position(Pos.CENTER);
notif1.showWarning();
}
Can somebody help me with using this notifications when I'm running jar?
Thanks!
I have fixed this isue by printing info to console. The problem was that there weren't two needed folders in final jar archive (I think it's IDEA bug) so I putted in them manually. After that I had javafx exception and I added this code
notif1.owner(chart3);
before
notif1.showWarning();
to avoid it.

Trouble running a Java.jar file with a Swing GUI on Android phone - Major.Minor Version 52.0 & Main Class Name Missing

Hello!
The goal I have is pretty simple: I want to create an executable .jar file which should be able to bring up a form (made using swing) and write the put-in information into a well structurized plain text file. Due to portability/mobility reasons I will have to run this on an Android phone.
Since Android unfortunately doesn't have native Java support and I am unable to code a real Android application myself, I tried to use a Java emulator. Unfortunately, it appears that a working Java Emulator is hard to find to begin with. JBED instantly crashes on my Medion X701. Netmite's Website, which also seems to offer a .jar to .apk converter, appears to be down. JBlend seems to have installed properly, but I do not know how to work with the program now that it presents me nothing but an empty screen with the caption "0 Java ME Application(s)". On the web, I could not find a proper manual. PhoneME's official website was Java.net, which is no longer active. But during my research, I could find an older Version of PhoneME which seems to be running well.
As a test, I created a simple jar file that shows a maximized JFrame. Unfortunately, it isn't able to run my simple executable Jar file on my phone. The main reason for that should be the aforementioned error, which implies that the interpreter there is probably Java version 1.7, since PhoneME's last release was in 2015. There are more error messages referring to an "unknown source", which I believe originate from the first error. I tried to recompile my jar file using Eclipse, by setting the Compiler compliance level to 1.7 and choosing the jre 1.7 before exporting my project. Unfortunately, this didn't change the error messages I got on PhoneME!
As my last attempt, I created a Startup.class file which does the very same thing as my jar file.
import javax.swing.*;
public class Startup {
public static void main(String[] args) {
JFrame fishForm = new JFrame("Questionnaire");
fishForm.setExtendedState(JFrame.MAXIMIZED_BOTH);
fishForm.setVisible(true);
}
}
I compiled it using the command prompt in order to make sure that it is indeed using java 1.7 as the target. This .class file, which ran fine on my computer, leads me into a different error: "Main class name missing.", followed by multiple lines of usage explaination. The command line PhoneME presents doesn't seem to allow me to post command operations myself, as pressing Enter on the virtual keyboard does nothing.
My Questions
Is it even possible to run a Java application on Android which relies on the GUI libraries of Swing, or am I wasting my time?
Is there a different, up-to-date Java emulator for Android which I could use? Or is there a working jar2apk converter somewhere out there?
If not, then there's probably a reason why Java emulators ceased to exist. Is there a much simpler solution for my initial problem which I am missing?
I'll await your ideas eagerly!

Creating a shortcut for a java application with arguments

Struggling to create a desktop shortcut for a jar file.
Found a solution which created a .url file aka
[InternetShortcut]
URL=D:/test.jar
which worked pretty well. But then the stakes went higher and now i need to run in from a certain JRE. Basically, what i'w tried, was to fix the URL to %javapath%/java.exe -jre d:/test.jar, but Win7 started to treat it as a web-url, adding a "http://" at the start of the url when looking through file properties.
Is there a way to make this work? Or maybe a freeware library that can create urls in a more graceful way?
Upd: Finally struck on an answer(and yes, i found in on stockoverflow) - Creating a shortcut file from Java
If someone gets here, looking for the answer:
ShellLink sl = ShellLink.createLink("C:/Program Files (x86)/Java/jre1.8.0_121/bin/javaw.exe");
sl.setCMDArgs("-jar D:/Test1.jar");

How to attach a javaagent to all java applets

I wrote a sample java agent that creates a "hello world" file in a specific place. I want it to run whenever any java applet is run (definitely including ones I didn't write myself.) How do I do that?
My agent works when I run it manually from the command line (as in, running another java file with the agent attached to it.) On the automatic front, I tried going into the java configuration panel -> Java -> View -> JRE Configuration Settings -> setting the optional arguments to attach my agent, but that doesn't seem to do the trick. I browse with FF to someplace that makes me run a java applet but no file is created.
I'm guessing I'm missing something basic here, because when I try to google this people all around various forums seem to answer it as if it's the most trivial thing: "just use -javaagent, read more about it in this (link)."
If it helps at all, this is the optional parameter I add in the configuration panel (which works when I use it manually):
-javaagent:C:\Users\admin\workspace\poc\bin\poc\myagent.jar
Update: I found out I can set an environment variable (JAVA_TOOL_OPTIONS) to -javaagent:(agentpath). Once again it works flawlessly with local java applications, but now when I browse over to a webpage with an applet, firefox auto-closes itself. IE declares the webpage broken. Chrome doesn't even display the applet. The agent itself at this point does nothing - it just has an empty premain method. Anyone?
Setting the environment variable JAVA_TOOLS_OPTIONS=-javagent:(path) is right.
My error was that I misconstructed the .jar file. Once I fixed that (simply used the jar.exe tool to make my .jar) it worked properly - locally and on all applets via firefox. Until then, that was what caused firefox to either hang or crash when encountering an applet.

Java Distribution Code vs Development Code

I have an application developed in Java that's almost ready for distribution. However, I have a problem switching from my development env to publishing env, and back to development. For instance, in Eclipse, if I just want to do a test run via the run button, I have to change the code so my JMenuItems show up.
In my development environment I had the following that worked well:
JMenuItem[] appItems = new JMenuItem[2];
appItems[0] = new JMenu("New");
appItems[0].setIcon(new ImageIcon(../POS_System/images/new_icon_sm.png")));
But, as I near deployment, to get this to work in the deployable JAR, I need to alter the code:
JMenuItem[] appItems = new JMenuItem[2];
appItems[0] = new JMenu("New");
appItems[0].setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/new_icon_sm.png")));
This is order to reach into the JAR and get the appropriate image.
I have a lot of these JMenuItems. I'd really like to be able to test the app via the run button in Eclipse, as well as create a JAR without changing the code.
Is there a simple way to do this? I thought the getResource method would still allow me to use the JMenus, JMenuItems etc, but they aren't available when I run the program from Eclipse. It seems silly that I would have to keep switching back and forth.
I appreciate any help here.
If you do not want to move your images into the src/ folder as you have suggested, then in Eclipse, you should update your Eclipse configuration under the Project Prooperties -> Java Build Path and add an additional source folder, this one pointing to where you keep your images. I believe this will fix it.

Categories