-1

I'm trying to launch my java file Test.java like this:

java -cp . gcomputing.Test

I'm here: /src and my Test.java file is here: /src/gcomputing/Test.java

My Test.java file :

package gcomputing;

public class Test {
  public static void main(String[] args) {
    System.out.println("Hi");
  }
}

The output is:

Error: Could not find or load main class gcomputing.Test

I already tried those links:

Community
  • 1
  • 1
Souin
  • 94
  • 1
  • 11
  • 2
    if your compiler didn't wok, you wouldn't get that error message. – Stultuske Jul 29 '16 at 13:06
  • So it's not my compiler. I knew that, but I don't know while it doesn't work. – Souin Jul 29 '16 at 13:07
  • Why do you have this package statement: package sismage.gcomputing; when your package is actually gcomputing ? – Stultuske Jul 29 '16 at 13:07
  • Rather than saying your question isn't a duplicate, how about saying which questions you read, tried, and didn't succeed using. – Andy Turner Jul 29 '16 at 13:07
  • @Stultuske my bad, it's not like that in the file. (i putted an other version of my file) – Souin Jul 29 '16 at 13:08
  • 1
    Did you compile it? you are trying to run it, the compile command is javac, not java – Stultuske Jul 29 '16 at 13:08
  • 1
    Maybe try reading the documentation: [Compile and run your first Java program](http://stackoverflow.com/documentation/java/84/compile-and-run-your-first-java-program#t=20160729130805337198). – Andy Turner Jul 29 '16 at 13:08
  • 2
    Firstly, if you encounter a situation where it is the case that either Java doesn't work or you are using Java incorrectly, it is probably the latter. – bradimus Jul 29 '16 at 13:09
  • 1
    If `sismage` is really not in your source file, then where does it come from? The compiler doesn't make such things up by itself... you probably forgot to save your source file in your editor, or you are compiling a different file than you really think you are compiling. – Jesper Jul 29 '16 at 13:21
  • I changed the "sismage" thing. It was a mistake. – Souin Jul 29 '16 at 13:24
  • 2
    How are you compiling? – bradimus Jul 29 '16 at 13:28

3 Answers3

0

I would suggest to go to the folder containing your file using cd command.

Then type:

javac Test.java

java Test

It should run the main method of your program.

L01c
  • 1,033
  • 10
  • 19
  • yes, but it seems that's not what he's trying to do. and it should work from a parent folder just as well – Stultuske Jul 29 '16 at 13:13
  • Yes but I can't use javac. Indeed, i'm in a professional computer and the already-implemented version works without it. Maybe, when we do a build in Eclipse it creates the .class files automaticaly? – Souin Jul 29 '16 at 13:24
0

java command is not compiler, it's runtime invocation program. Java compiler is javac

The correct invocation of compiler should look as following in your case:

$ javac src/gcomputing/Test.java
$ java -cp ./src gcomputing.Test
 Hi

You can find more details in Oracles' documentation

Robert Navado
  • 1,319
  • 11
  • 14
0

I found. It's my bad:

the Eclipse "build" created some .class file, and my Test file was in it. But not (why?) at the same place.

Found with the $find linux command.

Thank you and excuse me,

Clément

Souin
  • 94
  • 1
  • 11
  • 1
    "why?" because it's standard practice not to build things in your `src/` tree. It's much easier if you can just blat the build directory by itself, without worrying about source code mixed up with it. – Andy Turner Jul 29 '16 at 13:33