Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I´am trying to use this Finger.java for my Android project, But I get this error.

$java Finger username@host.com
Exception in thread "main" java.lang.NoClassDefFoundError: Finger
Caused by: java.lang.ClassNotFoundException: Finger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Finger. Program will exit.

I have looked on Google and then I try this:

java -classpath .finger

And...

set classpath=%classpath%:.Finger.java:

But this does not work :(

I get the Java file from this page: http://elm.eeng.dcu.ie/~ee402/ee402notes/html/ch10s03.html#FingerScreenGrab

I only need to call it from my 'mainActivity' for my project.

My Java setup is like this:

$java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.3) (6b24-1.11.3-1ubuntu0.12.04.1)
OpenJDK Server VM (build 20.0-b12, mixed mode)

and

$javac -version
javac 1.7.0_07
share|improve this question
    
hmm I have not compiled it... do I need to do this if I only need to call the methods from my 'mainActivity'? can I not just write 'import Finger;'? –  Voidcode Sep 2 '12 at 14:06
1  
To people considering voting to close this question: This question should probably not be closed as off-topic, since it appears to fundamentally come down to what the java and javac commands do, how to use them, and what kinds of files they can be used on. Those are not really programming questions. This appears to be a question on how to use the Java software in Ubuntu, rather than how to write Java code, so it would be considered on-topic for our site. –  Eliah Kagan Sep 2 '12 at 17:59
    
Ok sorry if this is too easy for this site. I have make a lot of java-code but I use Eclipse, I has never needed to think about compiled commands before now. The reason I try two run it in the terminal is to secure me that class work. The Finger.java file is an simple-program so I will my change it to return the output-data as string(but this is not an part of my question).. Ok now I have try compiled 'javac Finger.java' so the compiled-file is 'Finger.class'. Then I get this error(paste.ubuntu.com/1182163) look it up, and I only need to changes the JRE to openJDK-7 and then it runs:) –  Voidcode Sep 2 '12 at 19:26
    
Thank :) you got me thinking in the right direction.. –  Voidcode Sep 2 '12 at 19:32
    
I'm glad the problem is solved. I've posted an answer detailing the solution for the first problem (which is what you posted the question about). –  Eliah Kagan Sep 2 '12 at 19:51

1 Answer 1

up vote 4 down vote accepted

As discussed in comments...

Finger.java is a Java source code file. .java source code files cannot be run in that form; they must first be compiled.

As you've explained, Finger.java is intended to provide code that may be used as part of a Java program, and also is designed to function as a standalone application.

To use a class defined in a .java file in another program, it will have to be compiled, but this often need not be a separate build step from building your whole program. Typically, when you build your program, all the .java files in its source code are compiled. Integrated development environments like Eclipse ensures this happens, but you can do this from the command-line as well.

How you'll do it depends on whether or not you're using an automated build system (such as Ant or SCons). If you're not using an automated build system, you can still often build from the command-line with a single invocation of the javac command; the manpage for your javac implementation details how (run man javac). Since this is not the focus of your question, I won't go into any more detail here about how to build a Java application from multiple source code files.

Since any .java source code file will need to be compiled before it can be run, and you want to run Finger as a standalone program, you will have to first compile it:

javac Finger.java

Then you can run it:

java Finger

That's actually running the file called Finger.class that is created from compiling Finger.java with javac as detailed above. The java command cannot execute Java source code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.