• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Can't compile program through command line

 
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to compile my application to a JAR file through command line. I can successfully compile it through Eclipse.
The first thing I do is cd to the directory with all my java files.
Then I run: javac *.java
This is the output I get:

I looked online but there really isn't much out there.
I've attached an image of the structure of my java files.
If I cd straight to the GUIComponents folder and then run 'java *.java' in there, it compiles fine:
Screen-Shot-2019-12-29-at-11.52.57.png
[Thumbnail for Screen-Shot-2019-12-29-at-11.52.57.png]
 
Saloon Keeper
Posts: 6706
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently you have not added those two libraries to the classpath. The compile command should look something like

javac -cp GUIComponents.jar:commons-lang3.jar *.java

where you would use the actual names (or paths) of those libraries.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Apparently you have not added those two libraries to the classpath. The compile command should look something like

javac -cp GUIComponents.jar:commons-lang3.jar *.java

where you would use the actual names (or paths) of those libraries.


I still can't get it to work.

I compiled GUIComponents.java to a .jar.
I added both files to the build path using Eclipse, so the classpath file now looks like this:

But I am still getting the same errors.
What have I done wrong?
 
Tim Moores
Saloon Keeper
Posts: 6706
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That file would not be used if you compile on the command line. Either add the jars to the classpath as I suggested, or use Eclipse for compiling.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:That file would not be used if you compile on the command line. Either add the jars to the classpath as I suggested, or use Eclipse for compiling.


Oh ok - that makes sense.

Thank you!
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:That file would not be used if you compile on the command line. Either add the jars to the classpath as I suggested, or use Eclipse for compiling.


Sorry to bother you again, I tried the command you provided-
It fixed the issue of StringUtils, but I am getting another similar message with the GUIComponents:


Sorry if I am missing something obvious.
 
Tim Moores
Saloon Keeper
Posts: 6706
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You put GUIComponents twice in the import. Replace the second with a "*".
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:You put GUIComponents twice in the import. Replace the second with a "*".


There was only one import of GUIComponents, but I tried it anyway, and it didn't work.
I've provided a pastebin link to my MainGUI code (to avoid making the response long)
https://pastebin.com/JjtagQV7
 
Tim Moores
Saloon Keeper
Posts: 6706
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant was instead of importing "GUIComponents GUIComponents" you need to import "GUIComponents.*"
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bod McLeon wrote: but I tried it anyway, and it didn't work.


As I said, I did try, but with no luck.
I also tried other variations like GUIComponents.GUIComponents.Components and so on.
 
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you look inside the jar file to see what classes and their paths are there?  jar files are zip files and can be opened with a zip utility.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Can you look inside the jar file to see what classes and their paths are there?  jar files are zip files and can be opened with a zip utility.


There are two class files:
GUIComponents.class
GUIComponents$Components.class
and then, of course, META-INF.

I don't know what you mean by their paths.

EDIT:
I don't know how the classes are made, but I feel like the contents of GUIComponents$Components.class is not enough to represent what is in the original java file.
https://pastebin.com/eY8MvEEV - GUIComponents.java
https://pastebin.com/WPeMTskF - GUIComponents$Components.class
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D:\JavaDevelopment\Bridge_Projects\BuildCSV.class
The path is the folders from the root to the file.

Were those files at the root level or were they inside of other folders?

The path is what you code on the import statement so the compiler can find the .class file.
 
Tim Moores
Saloon Keeper
Posts: 6706
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bod McLeon wrote:
As I said, I did try, but with no luck.
I also tried other variations like GUIComponents.GUIComponents.Components and so on.


What you posted on pastebin is incorrect. Anything with GUIComponents twice in a row will not work.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:D:\JavaDevelopment\Bridge_Projects\BuildCSV.class
The path is the folders from the root to the file.

Were those files at the root level or were they inside of other folders?

The path is what you code on the import statement so the compiler can find the .class file.


The two class files I provided are from inside the JAR file. So no, they weren't at root level.

Tim Moores wrote:What you posted on pastebin is incorrect. Anything with GUIComponents twice in a row will not work.


You mean like: ?
As I said, I have already tried that. It didn't work.
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Were the class files at the top level within the jar file?
Or were they inside of folders?  The folders would be the path.

From the import statement you posted, the class files need to be in folder at the end of this path: com/subreddimages/GUIComponents

com would be at the top level,  it would contain the folder: subreddimages which would contain the folder: GUIComponents which would contain the class files.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Were the class files at the top level within the jar file?
Or were they inside of folders?  The folders would be the path.

From the import statement you posted, the class files need to be in folder at the end of this path: com/subreddimages/GUIComponents

com would be at the top level,  it would contain the folder: subreddimages which would contain the folder: GUIComponents which would contain the class files.


No, the class files were at top level in the JAR file.

You say from my import that the class files need to be in com/subreddimages/GUIComponents. So does that mean I have to create those folder within  the jar file?
At the moment, com/subreddimages/GUIComponents points to GUIComponents.java.
Unless you mean bin/com/subreddimages/GUIComponents, where yes, the class files are located.
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have to create those folder within  the jar file?


Yes the class file's location must match the path on the import statement.

What package statement is in  the GUIComponents.java source file?   It needs to match the above paths.


where yes, the class files are located.


Are you talking about the class file's location within the jar file?
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

I have to create those folder within  the jar file?


Yes the class file's location must match the path on the import statement.


Ok, I'll try it.

Norm Radder wrote:What package statement is in  the GUIComponents.java source file?   It needs to match the above paths.


The package statement is:

Norm Radder wrote:

where yes, the class files are located.


Are you talking about the class file's location within the jar file?


No, just the class files generate by Eclipse, under the /bin/ folder.

EDIT: Nope, unless I've done it wrong, it didn't work:
Take GUIComponents.jar: Opening it with unarchiver produced this file structure:

I changed it to:


Then zipped it, and converted it to a jar.
It didn't work.
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is path to the class file inside of the jar file?
Does it match the path on the import statement?
Is the class in a package that matches the path?
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:What is path to the class file inside of the jar file?
Does it match the path on the import statement?
Is the class in a package that matches the path?


The path to the class inside the jar file is what I just posted.

The class files are top level inside the jar file. It didn't match the import, so I changed it to this:

Now it does match the import statement.

The class isn't in a package that matches the path, however, I did try it.
None of these worked.
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The contents of the jar file must match the path on the import statement and the package statement.
If the top level is com for the import and package statements, then the top level folder in the jar file must be com.


It looks like the top level folder is GUIComponents, not com.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:The contents of the jar file must match the path on the import statement and the package statement.
If the top level is com for the import and package statements, then the top level folder in the jar file must be com.


It looks like the top level folder is GUIComponents, not com.


Sorry, that's my bad.
com is the top level. GUIComponents is just the folder that appears when I decompress the jar.
I think on Windows you can just view the file structure of the jar without having to decompress it. On Mac, you actually have to decompress it, meaning it creates a separate folder from the jar file. GUIComponents is just the name of that folder.
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the package matches the jarfile path and the import statements matches the package path
and the jar file is on the classpath,
then it should work.

My only suggestions are to check the spellings (java is case sensitive) and that the paths all actually match.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:If the package matches the jarfile path and the import statements matches the package path
and the jar file is on the classpath,
then it should work.

My only suggestions are to check the spellings (java is case sensitive) and that the paths all actually match.



I appreciate the help anyway.

It's a weird problem, and there's got to be a fix - I will keep trying.
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is on the commandline you are using?  Is the jar file referenced properly and found by the javac command?
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:What is on the commandline you are using?  Is the jar file referenced properly and found by the javac command?


The full command I am using is:

Before using this command, I use cd to move into the directory with all the jar files.
 
Tim Moores
Saloon Keeper
Posts: 6706
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't add class files to the classpath - only directories and jar/zip files. The directory should be at the top level of the hierarchy - so in your case it would be the one that contains the "com" subdirectory.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:You don't add class files to the classpath - only directories and jar/zip files.


That's a mistake - it is supposed to be jar file, I dragged the wrong file there when I was putting it together.

Tim Moores wrote:The directory should be at the top level of the hierarchy - so in your case it would be the one that contains the "com" subdirectory.


The file path is:

So I cd to subreddimages, because it contains all the files.
If I cd to src, it won't find java files.
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see the GUIComponents.jar file in the classpath (-cp) option on the commandline.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:I don't see the GUIComponents.jar file in the classpath (-cp) option on the commandline.


Not in the one I posted, since I made a mistake.
All the other times I have used the command, GUIComponents.jar has been used:


 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the javac command finding the jar files?  Add the -Xlint option to the command for warning messages:
javac -Xlint -cp /Users/everyone/Desktop/SubreddimagesV2_dev/GUIComponents.jar:/Users/everyone/Desktop/SubreddimagesV2_dev/commons-lang3-3.9.jar *.java


My test:


// D:\JavaDevelopment\Testing\ForumQuestions12>javac -Xlint -cp nojar.jar Winners.java
// warning: [path] bad path element "nojar.jar": no such file or directory

 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Is the javac command finding the jar files?  Add the -Xlint option to the command for warning messages:
javac -Xlint -cp /Users/everyone/Desktop/SubreddimagesV2_dev/GUIComponents.jar:/Users/everyone/Desktop/SubreddimagesV2_dev/commons-lang3-3.9.jar *.java


My test:


// D:\JavaDevelopment\Testing\ForumQuestions12>javac -Xlint -cp nojar.jar Winners.java
// warning: [path] bad path element "nojar.jar": no such file or directory


Not sure where my original reply went.
This is the command:
This is the output:
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are the commandline and the error messages in separate posts?  I would expect them to be together.  
My test copied from command prompt window:


C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe -cp . -Xlint -Xdiags:verbose TestCode25.java

TestCode25.java:46: error: package com.nowhere.noClass does not exist
import com.nowhere.noClass.*;   //  Testing
^
1 error

1 error(s)



 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Why are the commandline and the error messages in separate posts?  I would expect them to be together.  
My test copied from command prompt window:


C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe -cp . -Xlint -Xdiags:verbose TestCode25.java

TestCode25.java:46: error: package com.nowhere.noClass does not exist
import com.nowhere.noClass.*;   //  Testing
^
1 error

1 error(s)




No reason, just thought looked neater.
Here's the full full output:

 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming the javac command works the same on a Mac as on a PC.  The -Xlint option caused javac to issue a warning when it could not find a jar file given on the commandline.
Could you try an experiment on your Mac to see if that is true?  Change the name of the jar file on the commandline to one that does not exist, issue the command and see if there is a warning about a missing file.  See my earlier post.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:I'm assuming the javac command works the same on a Mac as on a PC.  The -Xlint option caused javac to issue a warning when it could not find a jar file given on the commandline.
Could you try an experiment on your Mac to see if that is true?  Change the name of the jar file on the commandline to one that does not exist, issue the command and see if there is a warning about a missing file.  See my earlier post.


It does seem to give the same error.
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, that would mean the javac command is finding the jar file.  
That then would say the path in the jar file does not match the package path and the import statement path.  Those paths must be checked to make sure they match.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:
That then would say the path in the jar file does not match the package path and the import statement path


It doesn't match:
Import statement:

The classes in the the jar file are top level - the paths don't match.

NEW
Import statement:

The classes in the jar file are:

They match exactly.
Surprise surprise, it doesn't work.
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It doesn't match:


Please explain what does not match.  The paths in the two import statements look the same to me.

The classes in the the jar file are top level - the paths don't match.


Please explain what does not match.



Can you make some small, simple source files that have the same package statements, imports and references to the imported classes for testing.  Do NOT post the full code.
The source files should all be less than 50 lines each.

Then for testing we would build a jar file and use this command:  javac -cp theJarFile.jar *.java
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic