Why does java assertion have no effect? [duplicate] - java

This question already has answers here:
Assertion not working
(3 answers)
Closed 2 years ago.
i have this code:
public class C {
public static void main(String[] args) {
assert (9 < 6):"wrong";
System.out.println("ok");
}
}
I compile with "javac C.java".
I execute with "java C".
It prints "ok". Why does it not work?
Best regards.

as assertions are usually used for debugging they need to be enabled explicitly. hence, add -ea or -enableassertions parameter when starting your program java -ea C

Related

JAVA program accepting Devanagari(UTF8) charaters [duplicate]

This question already has answers here:
Passing command line unicode argument to Java code
(7 answers)
Closed 5 years ago.
What settings are needed so that Java class main method can accept Marathi/Devanagarai/UTF-8 characters.
e.g.
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println("भारत");
}
In eclipse Java run arguments if I specify argument as "abc" it prints
abc
भारत
But If I specify Marathi/Devanagarai/UTF-8 string e.g. "कौशिक" then it prints
?????
भारत
What extra settings to be done in eclipse ?
Later I want to excute this program from jar using command and call it from PHP
e.g.
java -cp xyz.jar DevanagariTest कौशिक
What extra parameters will be needed at that time ?
I think it has something to do with your default system encoding. You can try to start the Java Programm with an additional Argument:
-Dfile.encoding=UTF-8

How this program compiled and run successfully? [duplicate]

This question already has answers here:
Java Label usage [duplicate]
(2 answers)
Closed 6 years ago.
The below program compiled successfully and run without any errors. As per my understanding is should have thrown error in line 4. Can somebody explain?
class Test{
public static void main(String args[]) {
// my favorite website is
http://www.stackoverflow.com/questions/ask
System.out.println("hello world");
}
}
No, it's not an error, "http:" works here as the name of the label and "//" starts the comments which is ignored.

how can i run my java program outside of eclipse on mac [duplicate]

This question already has answers here:
How do I compile and run a program in Java on my Mac?
(5 answers)
Closed 6 years ago.
package trying;
import java.util.Scanner;
public class calculation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a=input.nextInt();
int b=input.nextInt();
int d=a+b;
System.out.println(d);
}
}
First one should compile the java by using this command:
javac calculation.java
As second step one would run the program with this command:
java calculation
for a more detailed version of this answere i'll refer to the duplicate post: How do I compile and run a program in Java on my Mac?

Finding Main Class in "Hello World" program in Java [duplicate]

This question already has answers here:
How to execute a java .class from the command line
(7 answers)
Closed 6 years ago.
I have a seemingly simple program in Java, but when I run it, I get the error:
Error: Could not find or load main class
Here is my code.
public class HelloPrinter
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
I'm entirely new to the Java language, and I don't know what else to do. My program file is named "hello_world.java", and when attempting to run the program, I type in "java hello_world.java". Am I doing something fundamentally wrong? I've also attempted "java -cp hello_world", but that gave me the same error.
What am I doing wrong?
From what little information you've given, I'd say you're executing it the wrong way.
Make sure you run the following two commands:
javac HelloPrinter.java
java HelloPrinter
The first command compiles your source code into a .class file. The second command executes that class file.

Website name in java program [duplicate]

This question already has answers here:
My java code has an obvious error. Why does it compile and run?
(2 answers)
Why doesn't putting a random http link in code cause a compilation error? [duplicate]
(2 answers)
Closed 9 years ago.
public class Main
{
public static void main(String[] args)
{
http://google.com
System.out.println("Welcome");
}
}
Why is this code not getting a compilation error? and why did it get a compilation error if we write this url name after System.out.println("Welcome"); ??
Because it's a label followed by a comment.

Categories