Rectangle 27 26

I was having a similar issue with my very first java program.

java HelloWorld.class

Which resulted in the same error.

java HelloWorld

java - Could not find or load main class - Stack Overflow

java windows
Rectangle 27 7

There are several ways to get input from the user. Here in this program we will take Scanner Class to achieve the task. This Scanner class comes under java.util, hence the first line of the program is import java.util.Scanner; which allows the user to read values of various types in java. The import statement line should have to be in the first line the java program, and we proceed further for code.

in.nextInt();//It just reads the numbers

in.nextLine();// It get the String which user enters

To access methods in Scanner class create a new scanner object as "in". Now we use a one of its method that is "next". "next" method gets the string of text that a user enters on the keyboard.

here I'm using in.nextLine(); to get the String which user enters.

import java.util.Scanner;

class GetInputFromUser {
    public static void main(String args[]) {
        int a;
        float b;
        String s;

        Scanner in = new Scanner(System.in);
        System.out.println("Enter a string");
        s = in.nextLine();
        System.out.println("You entered string " + s);

        System.out.println("Enter an integer");
        a = in.nextInt();
        System.out.println("You entered integer " + a);

        System.out.println("Enter a float");
        b = in.nextFloat();
        System.out.println("You entered float " + b);
    }
}

java.util.scanner - How can I read input from the console using the Sc...

java java.util.scanner
Rectangle 27 6

There are several ways to get input from the user. Here in this program we will take Scanner Class to achieve the task. This Scanner class comes under java.util, hence the first line of the program is import java.util.Scanner; which allows the user to read values of various types in java. The import statement line should have to be in the first line the java program, and we proceed further for code.

in.nextInt();//It just reads the numbers

in.nextLine();// It get the String which user enters

To access methods in Scanner class create a new scanner object as "in". Now we use a one of its method that is "next". "next" method gets the string of text that a user enters on the keyboard.

here I'm using in.nextLine(); to get the String which user enters.

import java.util.Scanner;

class GetInputFromUser {
    public static void main(String args[]) {
        int a;
        float b;
        String s;

        Scanner in = new Scanner(System.in);
        System.out.println("Enter a string");
        s = in.nextLine();
        System.out.println("You entered string " + s);

        System.out.println("Enter an integer");
        a = in.nextInt();
        System.out.println("You entered integer " + a);

        System.out.println("Enter a float");
        b = in.nextFloat();
        System.out.println("You entered float " + b);
    }
}

java.util.scanner - How can I read input from the console using the Sc...

java java.util.scanner
Rectangle 27 49

Calling System.exit(0) (or any other value for that matter) causes the Java virtual machine to exit, terminating the current process. The parameter you pass will be the return value that the java process will return to the operating system. You can make this call from anywhere in your program - and the result will always be the same - JVM terminates. As this is simply calling a static method in System class, the compiler does not know what it will do - and hence does not complain about unreachable code.

return statement simply aborts execution of the current method. It literally means return the control to the calling method. If the method is declared as void (as in your example), then you do not need to specify a value, as you'd need to return void. If the method is declared to return a particular type, then you must specify the value to return - and this value must be of the specified type.

return would cause the program to exit only if it's inside the main method of the main class being execute. If you try to put code after it, the compiler will complain about unreachable code, for example:

public static void main(String... str) {
    System.out.println(1);
    return;
    System.out.println(2);
    System.exit(0);
}

will not compile with most compiler - producing unreachable code error pointing to the second System.out.println call.

Terminating a Java Program - Stack Overflow

java
Rectangle 27 47

Calling System.exit(0) (or any other value for that matter) causes the Java virtual machine to exit, terminating the current process. The parameter you pass will be the return value that the java process will return to the operating system. You can make this call from anywhere in your program - and the result will always be the same - JVM terminates. As this is simply calling a static method in System class, the compiler does not know what it will do - and hence does not complain about unreachable code.

return statement simply aborts execution of the current method. It literally means return the control to the calling method. If the method is declared as void (as in your example), then you do not need to specify a value, as you'd need to return void. If the method is declared to return a particular type, then you must specify the value to return - and this value must be of the specified type.

return would cause the program to exit only if it's inside the main method of the main class being execute. If you try to put code after it, the compiler will complain about unreachable code, for example:

public static void main(String... str) {
    System.out.println(1);
    return;
    System.out.println(2);
    System.exit(0);
}

will not compile with most compiler - producing unreachable code error pointing to the second System.out.println call.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Terminating a Java Program - Stack Overflow

java
Rectangle 27 36

Apache Commons Daemon will run your Java program as Linux daemon or WinNT Service.

process - How to Daemonize a Java Program? - Stack Overflow

java process daemon
Rectangle 27 7

#!/bin/bash

java program "$@"

Or if you want the bash to exit when java is called, use this:

#!/bin/bash

exec java program "$@"

(This replaces the bash process with the java process instead of waiting until java returns.)

bash script to run command-line java program - Stack Overflow

java bash
Rectangle 27 3

  • Well, first System.exit(0) is used to terminate the program and having statements below it is not correct, although the compiler does not throw any errors.
  • a plain return; is used in a method of void return type to return the control of execution to its parent method.

That's really odd. I'd have expected the compiler to throw an "Unreachable code" error.

Terminating a Java Program - Stack Overflow

java
Rectangle 27 3

  • Well, first System.exit(0) is used to terminate the program and having statements below it is not correct, although the compiler does not throw any errors.
  • a plain return; is used in a method of void return type to return the control of execution to its parent method.

That's really odd. I'd have expected the compiler to throw an "Unreachable code" error.

Terminating a Java Program - Stack Overflow

java
Rectangle 27 27

Use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.

public class Hello {
  static {
    System.out.println("Hello, World!");
  }
}

Of course, you can run the program as java Hello and you will see the message; however, the command will also fail with a message stating:

Exception in thread "main" java.lang.NoSuchMethodError: main

[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling System.exit(0) immediately after printing the message.

not working for me... java version is 1.8.0_65

How can you run a Java program without main method? - Stack Overflow

java
Rectangle 27 27

Use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.

public class Hello {
  static {
    System.out.println("Hello, World!");
  }
}

Of course, you can run the program as java Hello and you will see the message; however, the command will also fail with a message stating:

Exception in thread "main" java.lang.NoSuchMethodError: main

[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling System.exit(0) immediately after printing the message.

not working for me... java version is 1.8.0_65

How can you run a Java program without main method? - Stack Overflow

java
Rectangle 27 30

If you can't rely on Java Service Wrapper cited elsewhere (for instance, if you are running on Ubuntu, which has no packaged version of SW) you probably want to do it the old fashioned way: have your program write its PID in /var/run/$progname.pid, and write a standard SysV init script (use for instance the one for ntpd as an example, it's simple) around it. Preferably, make it LSB-compliant, too.

Essentially, the start function tests if the program is already running (by testing if /var/run/$progname.pid exists, and the contents of that file is the PID of a running process), and if not run

logfile=/var/log/$progname.log
pidfile=/var/run/$progname.pid
nohup java -Dpidfile=$pidfile $jopts $mainClass </dev/null > $logfile 2>&1

The stop function checks on /var/run/$progname.pid, tests if that file is the PID of a running process, verifies that it is a Java VM (so as not to kill a process that simply reused the PID from a dead instance of my Java daemon) and then kills that process.

When called, my main() method will start by writing its PID in the file defined in System.getProperty("pidfile").

One major hurdle, though: in Java, there is no simple and standard way to get the PID of the process the JVM runs in.

Here is what I have come up with:

private static String getPid() {
    File proc_self = new File("/proc/self");
    if(proc_self.exists()) try {
        return proc_self.getCanonicalFile().getName();
    }
    catch(Exception e) {
        /// Continue on fall-back
    }
    File bash = new File("/bin/bash");
    if(bash.exists()) {
        ProcessBuilder pb = new ProcessBuilder("/bin/bash","-c","echo $PPID");
        try {
            Process p = pb.start();
            BufferedReader rd = new BufferedReader(new InputStreamReader(p.getInputStream()));
            return rd.readLine();
        }
        catch(IOException e) {
            return String.valueOf(Thread.currentThread().getId());
        }
    }
    // This is a cop-out to return something when we don't have BASH
    return String.valueOf(Thread.currentThread().getId());
}

No need for the Java to write the PID, the script can do it as well (echo $! > /var/run/my.pid) Also, since usually only root can write to /var/run you can have the shell script running as root and the daemon run as another user.

Except you can't change uid or euid in java, so you have to do it from the shell, with sudo... it's a bit of a pain if you ask me. It's easier to have /var/run and var/log writable to the user you run as...

Also, the rationale for having Java write the pid file / lock file is to have a mean to know whether the initialization phase of the daemon completed successfully (by writing at the end of that phase).

This answer is useless. You want to do the fork AFTER the sockets are created. This just detaches the process from the shell, it's a different thing altogether.

process - How to Daemonize a Java Program? - Stack Overflow

java process daemon
Rectangle 27 701

Reasons why Java cannot find the class

First of all, you need to understand the correct way to launch a program using the java (or javaw) command.

java [ <option> ... ] <class-name> [<argument> ...]
<option>

where <option> is a command line option (starting with a "-" character), <class-name> is a fully qualified Java class name, and <argument> is an arbitrary command line argument that gets passed to your application.

<class-name>

What this is going to do is the following:

<argument>

The fully qualified name (FQN) for the class is conventionally written as you would in Java source code; e.g.

packagename.packagename2.packagename3.ClassName

However some versions of the java command allow you to use slashes instead of periods; e.g.

packagename/packagename2/packagename3/ClassName

which (confusingly) looks like a file pathname, but isn't one. Note that the term fully qualified name is standard Java terminology ... not something I just made up to confuse you :-)

Here is an example of what a java command should look like:

So why might it be unable to find the class? Basically, there are two main causes:

java -Xmx100m com.acme.example.ListUsers fred joe bert

The above is going to cause the java command to do the following:

  • Search for the compiled version of the com.acme.example.ListUsers class.
  • Check that the class has a main method with signature, return type and modifiers given by public static void main(String[]). (Note, the method argument's name is NOT part of the signature.)
  • Call that method passing it the command line arguments ("fred", "joe", "bert") as a String[].

When you get the message "Could not find or load main class ...", that means that the first step has failed. The java command was not able to find the class. And indeed, the "..." in the message will be the fully qualified class name that java is looking for.

The first likely cause is that you may have provided the wrong class name. (Or ... the right class name, but in the wrong form.) Considering the example above, here a variety of wrong ways to specify the class name:

Example #1 - a simple class name:

java ListUser

When the class is declared in a package such as com.acme.example, then you must use the full classname including the package name in the java command; e.g.

java com.acme.example.ListUser

Example #2 - a filename or pathname rather than a class name:

java ListUser.class
java com/acme/example/ListUser.class

Example #3 - a class name with the casing incorrect:

java com.acme.example.listuser
  • Example #4 - a typo java com.acme.example.mistuser

Example #5 - a source filename

/usr/local/acme/classes/com/acme/example/Foon.class
java ListUser.java

Example #6 - you forgot the class name entirely

java lots of arguments

The second likely cause is that the class name is correct, but that the java command cannot find the class. To understand this, you need to understand the concept of the "classpath". This is explained well by the Oracle documentation:

java [ <option> ... ] -jar <jar-file-name> [<argument> ...]
java
java -Xmx100m -jar /usr/local/acme-example/listuser.jar fred

So ... if you have specified the class name correctly, the next thing to check is that you have specified the classpath correctly:

  • Read the three documents linked above. (Yes ... READ them. It is important that a Java programmer understands at least the basics of how the Java classpath mechanisms works.)
  • Look at command line and / or the CLASSPATH environment variable that is in effect when you run the java command. Check that the directory names and JAR file names are correct.
  • If there are relative pathnames in the classpath, check that they resolve correctly ... from the current directory that is in effect when you run the java command.

Please feel free to add comments suggesting improvements or corrections.

  • Check that the class (mentioned in the error message) can be located on the effective classpath.
  • Note that the classpath syntax is different for Windows versus Linux and Mac OS.

The thing which helped me to identify the problem is: "Yes ... READ them" comment :)

When you put a directory on the classpath, it notionally corresponds to the root of the qualified name space. Classes are located in the directory structure beneath that root, by mapping the fully qualified name to a pathname. So for example, if "/usr/local/acme/classes" is on the class path, then when the JVM looks for a class called com.acme.example.Foon, it will look for a ".class" file with this pathname:

/usr/local/acme/classes/com/acme/example/Foon.class

You should explain directory structure and the proper directory to be in to run java programs. One of the most confusing things for me and I'm sure other newbie Java developers is the weird folder structure.

If you had put "/usr/local/acme/classes/com/acme/example" on the classpath, then the JVM wouldn't be able to find the class.

If your classes FQN is com.acme.example.Foon, then the JVM is going to look for "Foon.class" in the directory "com/acme/example":

To give a concrete example, supposing that:

com.acme.example.Foon
/usr/local/acme/classes/com/acme/example/
# wrong, FQN is needed
java Foon

# wrong, there is no `com/acme/example` folder in the current working directory
java com.acme.example.Foon

# wrong, similar to above
java -classpath . com.acme.example.Foon

# fine; relative classpath set
java -classpath ../../.. com.acme.example.Foon

# fine; absolute classpath set
java -classpath /usr/local/acme/classes com.acme.example.Foon
  • The -classpath option can be shortened to -cp in most Java releases. Check the respective manual entries for java, javac and so on.
  • Think carefully when choosing between absolute and relative pathnames in classpaths. Remember that a relative pathname may "break" if the current directory changes.

The classpath needs to include all of the other (non-system) classes that your application depends on. (The system classes are located automatically, and you rarely need to concern yourself with this.) For the main class to load correctly, the JVM needs to find:

  • all classes and interfaces that are referred to by means of variable or variable declarations, or method call or field access expressions.

(Note: the JLS and JVM specifications allow some scope for a JVM to load classes "lazily", and this can affect when a classloader exception is thrown.)

It occasionally happens that someone puts a source code file into the the wrong folder in their source code tree, or they leave out the package declaration. If you do this in an IDE, the IDE's compiler will tell you about this immediately. Similarly if you use a decent Java build tool, the tool will run javac in a way that will detect the problem. However, if you build your Java code by hand, you can do it in such a way that the compiler doesn't notice the problem, and the resulting ".class" file is not in the place that you expect it to be.

The alternative syntax used for "executable" JAR files is as follows:

java [ <option> ... ] -jar <jar-file-name> [<argument> ...]
java -Xmx100m -jar /usr/local/acme-example/listuser.jar fred

In this case the name of the entry-point class (i.e. com.acme.example.ListUser) and the classpath are specified in the MANIFEST of the JAR file.

A typical Java IDE has support for running Java applications in the IDE JVM itself or in a child JVM. These are generally immune from this particular exception, because the IDE uses its own mechanisms to construct the runtime classpath, identify the main class and create the java command line.

However it is still possible for this exception to occur, if you do things behind the back of the IDE. For example, if you have previously set up an Application Launcher for your Java app in Eclipse, and you then moved the JAR file containing the "main" class to a different place in the file system without telling Eclipse, Eclipse would unwittingly launch the JVM with an incorrect classpath.

In short, if you get this problem in an IDE, check for things like stale IDE state, broken project references or broken launcher configurations.

It is also possible for an IDE to simply get confused. IDE's are hugely complicated pieces of software comprising many interacting parts. Many of these parts adopt various caching strategies in order to make the IDE as a whole responsive. These can sometimes go wrong, and one possible symptom is problems when launching applications. If you suspect this could be happening, it is worth restarting your IDE.

I had this problem when I was trying to run a Class with a 3rd party library. I invoked java like this: java -cp ../third-party-library.jar com.my.package.MyClass; this does not work, instead it is necessary to add the local folder to the class path as well (separated by :, like this: java -cp ../third-party-library.jar:. com.my.package.MyClass, then it should work

After years of java programming I still managed to end up on this page. For me the issue was that the classpath syntax is OS-dependent. I'm kind of new to programming on Windows and had no idea.

Additional notes, point 2 save me! It is sad to see that java does not say it does not find an imported class, but instead the main class you're trying to run. This is misleading, although I'm sure there's a reason for that. I had the case where java knew exactly where my class is, however it couldn't find one of the imported classes. Instead of saying that, it complained about not finding my main class. Really, annoing.

@StephenC Great answer. Consider adding to your answer the suggestion to re-run the failing java.exe invocations with the -Xdiag flag for JDK versions >7. This tip is in the answer stackoverflow.com/a/39117892/110126.

java - What does "Could not find or load main class" mean? - Stack Ove...

java class main
Rectangle 27 6

Because System.exit() is just another method to the compiler. It doesn't read ahead and figure out that the whole program will quit at that point (the JVM quits). Your OS or shell can read the integer that is passed back in the System.exit() method. It is standard for 0 to mean "program quit and everything went OK" and any other value to notify an error occurred. It is up to the developer to document these return values for any users.

return on the other hand is a reserved key word that the compiler knows well. return returns a value and ends the current function's run moving back up the stack to the function that invoked it (if any). In your code above it returns void as you have not supplied anything to return.

Does it really return null? Is it nothing as void? You cannot return null in the method with return type as void.

You are correct, I'll change my answer. Thanks for pointing out the mistake.

You are welcome :)

Terminating a Java Program - Stack Overflow

java
Rectangle 27 662

Reasons why Java cannot find the class

First of all, you need to understand the correct way to launch a program using the java (or javaw) command.

java [ <option> ... ] <class-name> [<argument> ...]
<option>
<class-name>
<argument>

The fully qualified name (FQN) for the class is conventionally written as you would in Java source code; e.g.

packagename.packagename2.packagename3.ClassName

However some versions of the java command allow you to use slashes instead of periods; e.g.

packagename/packagename2/packagename3/ClassName

which (confusingly) looks like a file pathname, but isn't one. Note that the term fully qualified name is standard Java terminology ... not something I just made up to confuse you :-)

Here is an example of what a java command should look like:

java -Xmx100m com.acme.example.ListUsers fred joe bert

The above is going to cause the java command to do the following:

  • Search for the compiled version of the com.acme.example.ListUsers class.
  • Check that the class has a main method with signature, return type and modifiers given by public static void main(String[]). (Note, the method argument's name is NOT part of the signature.)
  • Call that method passing it the command line arguments ("fred", "joe", "bert") as a String[].

When you get the message "Could not find or load main class ...", that means that the first step has failed. The java command was not able to find the class. And indeed, the "..." in the message will be the fully qualified class name that java is looking for.

The first likely cause is that you may have provided the wrong class name. (Or ... the right class name, but in the wrong form.) Considering the example above, here a variety of wrong ways to specify the class name:

Example #1 - a simple class name:

java ListUser

When the class is declared in a package such as com.acme.example, then you must use the full classname including the package name in the java command; e.g.

java com.acme.example.ListUser

Example #2 - a filename or pathname rather than a class name:

java ListUser.class
java com/acme/example/ListUser.class

Example #3 - a class name with the casing incorrect:

java com.acme.example.listuser
  • Example #4 - a typo java com.acme.example.mistuser

Example #5 - a source filename

java ListUser.java

Example #6 - you forgot the class name entirely

java lots of arguments

The second likely cause is that the class name is correct, but that the java command cannot find the class. To understand this, you need to understand the concept of the "classpath". This is explained well by the Oracle documentation:

java

So ... if you have specified the class name correctly, the next thing to check is that you have specified the classpath correctly:

  • Read the three documents linked above. (Yes ... READ them. It is important that a Java programmer understands at least the basics of how the Java classpath mechanisms works.)
  • Look at command line and / or the CLASSPATH environment variable that is in effect when you run the java command. Check that the directory names and JAR file names are correct.
  • If there are relative pathnames in the classpath, check that they resolve correctly ... from the current directory that is in effect when you run the java command.
  • Check that the class (mentioned in the error message) can be located on the effective classpath.
  • Note that the classpath syntax is different for Windows versus Linux and Mac OS.

When you put a directory on the classpath, it notionally corresponds to the root of the qualified name space. Classes are located in the directory structure beneath that root, by mapping the fully qualified name to a pathname. So for example, if "/usr/local/acme/classes" is on the class path, then when the JVM looks for a class called com.acme.example.Foon, it will look for a ".class" file with this pathname:

/usr/local/acme/classes/com/acme/example/Foon.class

If you had put "/usr/local/acme/classes/com/acme/example" on the classpath, then the JVM wouldn't be able to find the class.

If your classes FQN is com.acme.example.Foon, then the JVM is going to look for "Foon.class" in the directory "com/acme/example":

To give a concrete example, supposing that:

com.acme.example.Foon
/usr/local/acme/classes/com/acme/example/
# wrong, FQN is needed
java Foon

# wrong, there is no `com/acme/example` folder in the current working directory
java com.acme.example.Foon

# wrong, similar to above
java -classpath . com.acme.example.Foon

# fine; relative classpath set
java -classpath ../../.. com.acme.example.Foon

# fine; absolute classpath set
java -classpath /usr/local/acme/classes com.acme.example.Foon
  • The -classpath option can be shortened to -cp in most Java releases. Check the respective manual entries for java, javac and so on.
  • Think carefully when choosing between absolute and relative pathnames in classpaths. Remember that a relative pathname may "break" if the current directory changes.

The classpath needs to include all of the other (non-system) classes that your application depends on. (The system classes are located automatically, and you rarely need to concern yourself with this.) For the main class to load correctly, the JVM needs to find:

  • all classes and interfaces that are referred to by means of variable or variable declarations, or method call or field access expressions.

(Note: the JLS and JVM specifications allow some scope for a JVM to load classes "lazily", and this can affect when a classloader exception is thrown.)

It occasionally happens that someone puts a source code file into the the wrong folder in their source code tree, or they leave out the package declaration. If you do this in an IDE, the IDE's compiler will tell you about this immediately. Similarly if you use a decent Java build tool, the tool will run javac in a way that will detect the problem. However, if you build your Java code by hand, you can do it in such a way that the compiler doesn't notice the problem, and the resulting ".class" file is not in the place that you expect it to be.

The alternative syntax used for "executable" JAR files is as follows:

java [ <option> ... ] -jar <jar-file-name> [<argument> ...]
java -Xmx100m -jar /usr/local/acme-example/listuser.jar fred

In this case the name of the entry-point class (i.e. com.acme.example.ListUser) and the classpath are specified in the MANIFEST of the JAR file.

A typical Java IDE has support for running Java applications in the IDE JVM itself or in a child JVM. These are generally immune from this particular exception, because the IDE uses its own mechanisms to construct the runtime classpath, identify the main class and create the java command line.

However it is still possible for this exception to occur, if you do things behind the back of the IDE. For example, if you have previously set up an Application Launcher for your Java app in Eclipse, and you then moved the JAR file containing the "main" class to a different place in the file system without telling Eclipse, Eclipse would unwittingly launch the JVM with an incorrect classpath.

In short, if you get this problem in an IDE, check for things like stale IDE state, broken project references or broken launcher configurations.

It is also possible for an IDE to simply get confused. IDE's are hugely complicated pieces of software comprising many interacting parts. Many of these parts adopt various caching strategies in order to make the IDE as a whole responsive. These can sometimes go wrong, and one possible symptom is problems when launching applications. If you suspect this could be happening, it is worth restarting your IDE.

I had this problem when I was trying to run a Class with a 3rd party library. I invoked java like this: java -cp ../third-party-library.jar com.my.package.MyClass; this does not work, instead it is necessary to add the local folder to the class path as well (separated by :, like this: java -cp ../third-party-library.jar:. com.my.package.MyClass, then it should work

After years of java programming I still managed to end up on this page. For me the issue was that the classpath syntax is OS-dependent. I'm kind of new to programming on Windows and had no idea.

Additional notes, point 2 save me! It is sad to see that java does not say it does not find an imported class, but instead the main class you're trying to run. This is misleading, although I'm sure there's a reason for that. I had the case where java knew exactly where my class is, however it couldn't find one of the imported classes. Instead of saying that, it complained about not finding my main class. Really, annoing.

@StephenC Great answer. Consider adding to your answer the suggestion to re-run the failing java.exe invocations with the -Xdiag flag for JDK versions >7. This tip is in the answer stackoverflow.com/a/39117892/110126.

java - What does "Could not find or load main class" mean? - Stack Ove...

java class main
Rectangle 27 20

javac is the Java compiler. java is the JVM and what you use to execute a Java program. You do not execute .java files, they are just source files. Presumably there is .jar somewhere (or a directory containing .class files) that is the product of building it in Eclipse:

java
webcall/classes
webclass\lib

i used javac -cp extjarfiles/AllJarFiles main.java ------ It compiled successfully but getting error in running the project

You need to compiler both the Sub.java and main.java and the locate the directory where the Sub.class and main.class files exist.

no i dont have such folder

compilation - How to run Java program in command prompt - Stack Overfl...

java compilation command-line-arguments command-prompt
Rectangle 27 1

Add bracket for IF condition

if (inList(in,numbersArray))
{
            numbersArray[n]=in;
            n++;
}

You should always uses braces like that even for one liners.

I'm still not getting the correct data.. It's adding numbers into the array that there are two of in the text file. No idea whats going on

@user102817 i run on my system and after this change it is working fine.I guess u r having concern y many values are 0? the answer is all the values of array of type double is initialized with default value and default value of double is 0.0

I'm still not sure where im going wrong. im using the data 5 6 7 8 9 0 9 8 0 and am getting... The unique numbers were [5.0, 6.0, 7.0, 8.0, 9.0] in return. Obviously 8 and 9 shouldn't be there since they aren't the only 8 or 9 in the list.

First Java Program: Random data in my Array? - Stack Overflow

java arrays loops methods boolean
Rectangle 27 1

if (inList(in,numbersArray)){
            numbersArray[n]=in;
            n++;
}
if (inList(in,numbersArray))
                numbersArray[n++]=in;

First Java Program: Random data in my Array? - Stack Overflow

java arrays loops methods boolean
Rectangle 27 18

javac is the Java compiler. java is the JVM and what you use to execute a Java program. You do not execute .java files, they are just source files. Presumably there is .jar somewhere (or a directory containing .class files) that is the product of building it in Eclipse:

java
webcall/classes
webclass\lib

i used javac -cp extjarfiles/AllJarFiles main.java ------ It compiled successfully but getting error in running the project

You need to compiler both the Sub.java and main.java and the locate the directory where the Sub.class and main.class files exist.

no i dont have such folder

compilation - How to run Java program in command prompt - Stack Overfl...

java compilation command-line-arguments command-prompt
Rectangle 27 6

Because System.exit() is just another method to the compiler. It doesn't read ahead and figure out that the whole program will quit at that point (the JVM quits). Your OS or shell can read the integer that is passed back in the System.exit() method. It is standard for 0 to mean "program quit and everything went OK" and any other value to notify an error occurred. It is up to the developer to document these return values for any users.

return on the other hand is a reserved key word that the compiler knows well. return returns a value and ends the current function's run moving back up the stack to the function that invoked it (if any). In your code above it returns void as you have not supplied anything to return.

Does it really return null? Is it nothing as void? You cannot return null in the method with return type as void.

You are correct, I'll change my answer. Thanks for pointing out the mistake.

You are welcome :)

Terminating a Java Program - Stack Overflow

java