0

I want to compile a bunch of .java files with a library .jar file. Following this topic, my previous GUI program runs fine. However, this command line program fails!

$ /opt/jdk1.8.0_131/bin/javac -classpath lib/*.jar *.java
$ ls
Module1.class  Cross.class  Ora.java  Safe.class
Module1.java   Cross.java   p1/       Safe.java
lib/           Ora.class      
$ grep main Module1.java
   public static void main(String[] args) {
$ ls lib
jsoup-1.10.3.jar
$ /opt/jdk1.8.0_131/bin/java -classpath .:lib/* Module1
Error: Could not find or load main class Module1

How can I fix that?

mahmood
  • 23,197
  • 49
  • 147
  • 242

1 Answers1

1

Does Module1.java have no package statement? If there is a package statement then it would need to be moved into the package folder and run with a command like

/opt/jdk1.8.0_131/bin/java -classpath .:lib/* package.Module1

I didn't know classpath would support wildcards but looks like that added that in java 6 which shows how long since I have run java from the command line.

Add the body of Module1.java to your question if you are not sure what I am asking.

Steve Bauer
  • 179
  • 10
  • The `package Moule1;` is added at the top of the all java files. As you can see there is no package file what I have to move to package folder. – mahmood Aug 02 '17 at 16:50
  • So, I removed the package statement and it is now fine! I don't think I need a package now – mahmood Aug 02 '17 at 16:50
  • If the package is Module1 then the class file needs to be in Module1 directory and the java command would be `/opt/jdk1.8.0_131/bin/java -classpath .:lib/* Module1.Module1` The convention is to create your .java files in the package directory so when the compiler creates the class file the directory structure for the packages is already in place. You will be fine without a package for a simple test program but it has limits in how it can be used. – Steve Bauer Aug 02 '17 at 17:42