Java code to find operating system - java

I am very new at coding in Java and I want to write a code to find out my operating system through a dialogue box (Windows). I wrote this code but I am not sure how to completely implement it.
Please let me know what I am doing wrong in simple terms as I am a beginner.
import java.awt.*;
import javax.swing.*;
public class JavaTut {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null,System.getProperty("os.name").list(System.out));
Toolkit.getDefaultToolkit().beep();
}
}

Instead of System.getProperty("os.name").list(System.out), use System.getProperty("os.name").
Example main method:
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, System.getProperty("os.name"));
}

Related

How do I use j2html without rendering everything

I am converting my html rendering code to use j2html. Whilst I like the library it is not easy for me to convert all the code in one go so sometimes I may convert the outer html to use j2html but not able to convert the inner html to j2html at the same time. So I would like j2html to be able to accept text passed to it as already rendered, but it always re-renders it so
System.out.println(p("<b>the bridge</b>"));
returns
<p><b>the bridge</b></p>
is there a way I get it to output
<p><b>the bridge</b></p>
Full Test Case
import j2html.tags.Text;
import static j2html.TagCreator.b;
import static j2html.TagCreator.p;
public class HtmlTest
{
public static void main(String[] args)
{
System.out.println(p(b("the bridge")));
System.out.println(p("<b>the bridge</b>"));
}
}
import static j2html.TagCreator.b;
import static j2html.TagCreator.p;
import static j2html.TagCreator.rawHtml;
public class HtmlTest
{
public static void main(String[] args)
{
System.out.println(p(b("the bridge")));
System.out.println(p(rawHtml("<b>the bridge</b>")));
}
}
Result:
<p><b>the bridge</b></p>
<p><b>the bridge</b></p>
In j2html 1.1.0 you can disable text-escaping by writing
Config.textEscaper = text -> text;
Be careful though..

Why can't I run the all java classes?

I'm doing a port over of codes from Processing to Netbeans Java. I'm having problems running multiple java classes. My codes are spread out into 14 classes, where my main class include only this set of codes:
package gardeningmania;
import java.io.File;
import processing.core.*;
public class GardeningMania extends PApplet{
public static void main(String[] passedArgs) {
File currentDir = new File("."); getAllFilse(currentDir);
PApplet.main(new String[]{/*"--present",*/"gardeningmania.GardeningMania"});
}
public static void getAllFilse(File currentDir) {
File[] filesList = currentDir.listFiles();
for(File f : filesList){
if(f.isDirectory()) getAllFilse(f);
if(f.isFile()){ System.out.println(f.getName()); }
}
}
}
Whenever the project is being run, only a small screen will pop-up with a gray background, however, that's all it shows. It seems to me that it's unable to read all my codes from the other 13 classes. Any ideas, anyone?
Applets don't use a main()-method; you have to implement other Methods instead:
void init();
void start();
void stop();
void destroy();
By default, the class Applet doesn't implement them, they all have only an empty block.
Please see:
Why do applets not need a main()?
Java Applet runs without a main method?
Lesson: Java Applets
Life Cycle of an Applet

JButton cannot be resolved to a type

I got an error on the line a JButton is on. Can someone help? It's really short code, by the way:
public class Main {
public JButton[] grid = new JButton[9];
public void init_components() {}
public void init_icons() {}
public static void main(String[] args) {}
}
You need an appropriate 'import' statement in your source code to tell Java what 'JButton' means.
import javax.swing.JButton;
An IDE, like Eclipse, will make it much easier for you to get started here.

Jar Maker -- Which is the main class?

Alrighty, so I'm working on making a .jar for a client for a little game and I know how to use everything and have done this before, on windows, now i'm on a mac. This shouldn't make a difference but incase you wanted to know, there you go.
Now, I have a folder in eclipse named client, now normally the client.java is the main class but there is another named EGUI, this has the "public static void main(String[] args)", but in my client.java file, it also has a method like this:
public static final void main(String args[])
{
try
{
anInt957 = 0;
anInt958 = 0;
method52(false);//highmem
aBoolean959 = true;//members
signlink.storeid = 32;
signlink.startpriv(InetAddress.getLocalHost());
client client1 = new client();
client1.method1(503, false, 765);
setserver(args[0], "5555");
return;
}
catch(Exception exception)
{
return;
}
}
I guess my question is, does the "final" make it the main file? Or would it still be the EGUI, which looks like this:
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class EGUI
{
public static void main(String args[])
{
client.main(new String[] {
"127.0.0.1", "127.0.0.1", "127.0.0.1"
});
}
}
So, what i'm asking for is, why is it that when I'm setting the main file to EGUI, it isnt working? the applet opens up, but I keep getting an "error connecting to server" message every time, when I run it through terminal by copying the run.bat info and pasting that, it works perfectly! Any help is greatly appreciated!
public static void main(String args[]) means you can execute the class from the commandline. The final keyword means the method cannot be overridden by a sub class.
In your case this does not make it the jar's main execution class. The main class is set in META-INF/MANIFEST.MF. Normally it should have a line:
Main-Class: classname
but then with the actual class.
So open the jar with a zip program, and check MANIFEST.MF.
Your client.java has a main method, for testing purposes I suppose.

Java: Can't get my main method to work

I'm using Fedora and I have had some issues to get javac to work (I finally succeeded by making an alias). But now I can't execute my java code. I get the error in the title.
Here is the class that contains the main method:
public class test
{
public static void main(String args)
{
int res[]= {4,2,6};
res=Trieur.tri(res);
for(int i: res)
System.out.println(i);
}
}
I've been trying a lot of solutions in this forum but none seems to work. The program compiles successfully.
Can you please help me?
change this:
public static void main(String args[])
Or as public static void main(String[] args). Either syntax is valid in Java, although this format is arguably slightly more popular.

Categories