2

Similar question has been asked several times before but none helped me. Please provide solution.

This works fine in eclipse but I am unable to run it in cmd(Windows)

I have created an jar file with few classes which have some static methods (no main class inside jar) and I want to import it on an java file(which have main()) and run it.

It compiles successfully but while executing it gives error

could not find or load main class test

both jar file and java file are inside same directory

to compile I am doing

javac -cp file.jar test.java

to execute

java -cp file.jar test

I have tried followings while searching different answers but none worked:

java -cp file.jar:. test
java -cp .:file.jar test

Please guide me what I am doing wrong.

Here's test.java:

import pkg.classname;
class test {
    public static void main(String[] args) {
        ...
    }       
}

Here's the output of jar tf:

$ jar tf file.jar

META-INF/MANIFEST.MF
pkg/classname.class
pkg/others.class

PS:I intentionally tried giving invalid jar file name but the error is same.

raghuv
  • 59
  • 2
  • 7
  • Please do not close the question too soon I promises to remove if asked as soon as it solves my problem. – raghuv Mar 01 '15 at 06:36
  • Do you have a `package` declaration in test.java? – laune Mar 01 '15 at 06:37
  • add output of ' jar tvf file.jar ' to the question. And location of test.class. Could you show the test.java? – Jayan Mar 01 '15 at 06:38
  • yes the class are inside an package. – raghuv Mar 01 '15 at 06:38
  • @Jayan Sorry I cant understand what you were trying to ask. – raghuv Mar 01 '15 at 06:39
  • The class will be then in pkg/classname directory under the current one. java -cp file.jar;. pkg.classname.test should do. Since you are learning, please read about naming conventions. class name could be "Test", for example – Jayan Mar 01 '15 at 06:45
  • @Jayan can you please make an answer,And I will take care of the naming conventions in future. – raghuv Mar 01 '15 at 06:46
  • 1
    IMHO, just delete the question. – Jayan Mar 01 '15 at 06:46
  • Well, I answered this 7 minutes ago. – laune Mar 01 '15 at 06:48
  • Use `jar tf` to dump the symbols in the JAR file, and then post the results. E.g., `jar tf file.jar | grep "main"`. I had a similar problem that I was never able to solve: [Class is present in JAR, but still “Could not find or load main class”](http://stackoverflow.com/q/27951908). – jww Mar 01 '15 at 06:51
  • @jww `META-INF/MANIFEST.MF pkg/classname.class pkg/others.class` – raghuv Mar 01 '15 at 06:58

2 Answers2

0

If you have a Test.java:

package my.pack;
public class Test {...}

this file should be in a subfolder my\pack below the folder where you execute the stuff:

java -cp .;file.jar my.pack.Test

If you do not have a package statement in class Test (or test),

java -cp .;file.jar Test
java -cp .;file.jar test

should work.

laune
  • 31,114
  • 3
  • 29
  • 42
  • 1
    Well, you called it test - please adjust. And is it really in the subfolder? It agrees with Jayan's comment... – laune Mar 01 '15 at 06:49
  • You code doesn't show a `package` statement in class test. – laune Mar 01 '15 at 06:51
  • `Error: Could not find or load main class pkg.classname.test` – raghuv Mar 01 '15 at 06:53
  • I tried this `java -cp .;file.jar pkg.classname.test` – raghuv Mar 01 '15 at 06:54
  • and there is an class called `classname.class` inside pkg package. – raghuv Mar 01 '15 at 06:56
  • A class cannot be called `...class`. Please add the `package` (!!) statement to the code in your Q. – laune Mar 01 '15 at 06:57
  • Actully I am making this up,due to some reasons I don't wanna revel the real name of class and package ,I hope it wont matter a lot. – raghuv Mar 01 '15 at 07:02
  • You can change the names, but please be consistent, don't add impossible name parts ("class"), don't omit them entirely. And don't use "classname" as a replacement - it is utterly confusing. Use first letters only, e.g. foo.bar.acme => f.b.a – laune Mar 01 '15 at 07:05
  • Ok I will make shure to not to repeat it in future questions.(well I just tried to create an hello world program with class name as Classname and it worked well). – raghuv Mar 01 '15 at 07:09
  • *"Actully I am making this up,due to some reasons I don't wanna revel the real name of class and package ,I hope it wont matter a lot"* - I think this question should be closed since you can't provide real, accurate information. Its wasting everyone's time. Come back when you can provide accurate information. – jww Mar 01 '15 at 07:11
  • 1
    SO WILL YOU PLEASE PROVIDE THE package STATEMENT TO YOUR CLASS test IN YOUR QUESTION? OR DELETE THIS. – laune Mar 01 '15 at 07:13
  • A class pack.Classname.test is of course possible. But its (full) classname is `pack.Classname.test` and its simple name is `test`. So if you write "classname", what do you think this is? – laune Mar 01 '15 at 07:16
  • I don't know what to say and how to say but I should `java -cp .;file.jar test` Worked ,I am extremely sorry for wasting all of yours time,I don't know why it didn't previously probably I was doing something wrong,Anyways Thanks a lot for helping. – raghuv Mar 01 '15 at 07:30
  • You might have avoided answering "yes the class are inside an package." after my question regarding the package of class `test`. – laune Mar 01 '15 at 07:47
0

If your main Java class (test) is declared to be in package pkg and it's not in a ".jar", but just a "test.class" file, then:

  1. Make an immediate sub-directory pkg

  2. Put the main test.class file in that sub-directory

  3. Assuming your supporting library is support.jar, run:

    java -cp .;support.jar pkg.test

BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • Fair enough, I just didn't know how to demonstrate a long-ish process clearly in a comment. I should delete? – BaseZen Mar 01 '15 at 07:06
  • 1
    Yeah, that's one of those Stack Overflow pain points. I think this question is pretty much a waste of everyone's time because Raghuv is not providing accurate information. I'm moving to close the question. Lets put it out of its misery (or our misery). – jww Mar 01 '15 at 07:14