Nov 30

While program execution parameters can be passed which the program is picking up. Let’s take – as usual – an example;

Program Test has the following code:

public class Test{

public static void main(String args[]) {

for (String s:args)

System.out.println(s);

}

}

Starting the program with

java Test 1 2 3

leads to the Output:

1
2
3

String args [ ] is overtaking the passed values.

Declaration of main public static void main(String args[]) is not the one and only valid one. Others may be as well suitable, e.g.

public static void main(String … x)

static public main (String [ ] y)

static public void main (String [ ] y)

Tagged with:
Nov 28

with -D system-properties can be set for execution:

java -D<variable-name>=<property-value> <main class>

or with example-values

java -DmyValue=my Test

So we have set the system property “my Value” with the value “my” for the run of Test created. It is also possible to have values with spaces (e.g. my and yours). Therefore you have to set the String in quotes:

java -DmyValue=”my and yours” Test

If preferred those system properties can be set directly in code. The responsible class is java.util.Properties:

Properties p = System.getProperties();
p.setProperty(“myValue”,”my”);
//system property myValue get the value “my”

You can even list all system properties with

p.list(System.out);

A single value can be retrieved with

System.out.println( p.getProperty(“myValue”));


Tagged with:
Nov 26

As already explained a program is started with

java <filename> oder java <option> <filename>

One Option as already explained in context with assertions (and already mentioned several times in recent posts) is option -ea to enable assertions and therefore can be used. The opposite to this is -da to disable them.

With java -help more options are shown.

Tagged with:
Nov 24

With -d you can give the pathname where the file to be produced with javac should be written in

C:\Buchhalter>javac -d ../Angestellter Buchhalter.java

The file Buchhalter.class is written to  C:/Angestellter and not to C:/Buchhalter

It is also possible to say from different paths to where (here: folder Angestellter) which file (here Buchhalter.java from the folder Buchhalter) should be produced.

C:\>javac -d Angestellter Buchhalter/Buchhalter.java

In case the target-folder does not exist, it is not created, but a CompilerError produced.

Tagged with:
Nov 22

As already explained, a path can be given where the class is stored, let’s have a look and go a little bit deeper on an example:

File Angestellter.java stored in path C:\Angestellter  contains following code:

public class Angestellter{ public static void main(String args[]) { Buchhalter b = new Buchhalter(); } }

File Buchhalter.java stored in C:\Buchhalter contains following code:

public class Buchhalter{ public Buchhalter () { System.out.println(“Buchhalter angelegt”);}}

Let’s now go to C:\Buchhalter and compile with

javac Buchhalter.java

to get a class-file.

In path C:\Angestellter the to be created programm need to access the files from Buchhalter.java. Therefore we need to tell the compiler, to have a look into C:\Buchhalter and to include its class-file:

javac -classpath “C:\Buchhalter” Angestellter.java

Since it is only checked via compilation if methods exists and method-calls fit into each other we have to give the classpath again while executing the program.

Here we have something special; it should be looked into 2 class paths; the current one and the one from Buchhalter. Is the current one not with

set CLASSPATH=.

or

set CLASSPATH=C:\Angestellter

set, we have to give this path during programm-call. Multiple classpath will be divided through “;”

java -classpath “.;C:\Buchhalter” Angestellter

On Unix-based systems we have to use ‘:’  instead of ‘;’ and ‘\’ instead of ‘/’

Tagged with:
Nov 20

While compiling several options can be used, eg

javac -classpath <path> <name of file to be compiled> – giving a path-name where further class-files are stored

javac -d <path> – pathname, where the to be produced file should be stored

javac -help – no compilation, but giving a list of all possible options

Tagged with:
Nov 18

For those who I asked to stay tuned – now we reached the point to go deeper and deeper into DOS-compilation.

Unfortunately for SCJP we need to go back to the good(?) old DOS-compilation-standards.

The code will be directly saved in a file ending with .java and compiled with javac, eg

javac Test.java

To start the compiled program, we have to use java

java Test

Tagged with:
Nov 16

On which point(s) would an exception occur?

String PersNr = null;

if ( PersNr != null && PersNr.length() > 0)
System.out.println(“PersNr != null & PersNr.length() > 0″);

if ( PersNr != null | PersNr.length() > 0)
System.out.println(“PersNr != null | PersNr.length() > 0″);

if ( PersNr != null || PersNr.length() > 0)
System.out.println(“PersNr != null & PersNr.length() > 0″);

if ( PersNr != null & PersNr.length() > 0)
System.out.println(“PersNr != null & PersNr.length() > 0″);

Antwort/ answer
Tagged with:
Nov 14

Is this code compiling faultless?

class NewException extends IOException {

void test() throws NeueException {

throw NeueException();

}

}



Antwort/ answer

Tagged with:
Nov 12

Which exceptions are thrown by JVM and which are related to programmer?

  1. AssertionError
  2. ClassCastException
  3. NullPointerException
  4. IllegalStateException
  5. ExceptionInInitializerError
  6. StackOverflowError
  7. NoClassDefFoundError
  8. IllegalArgumentException
  9. ArrayIndexOutOfBoundsException
  10. NumberFormatException
Antwort/ answer
Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs