Having trouble compiling my java project. Help?

  • Context: Java 
  • Thread starter Thread starter MarcL
  • Start date Start date
  • Tags Tags
    Java Project
Click For Summary

Discussion Overview

The discussion revolves around issues related to compiling a Java project using the command line. Participants are addressing problems encountered when trying to compile multiple Java classes, specifically focusing on file naming conventions and command line usage.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports an error when attempting to compile a Java file, indicating that the file "HelloWorld.java" could not be found, despite successfully compiling another program previously.
  • The same participant shares code for two Java classes, "SongsAndApps" and "MyFirstProgram", expressing confusion about why the compilation fails after adding the second class.
  • Another participant notes that the class name must match the filename, suggesting that the filename for the "SongsAndApps" class should be "SongsAndApps.java".
  • A different participant points out the ambiguity in the original post, highlighting that the participant is trying to compile a "HelloWorld" program that is not included in the provided code examples.
  • This participant also explains that using an IDE like Eclipse simplifies the compilation process for multiple classes, and suggests a command line approach for compiling both classes together.

Areas of Agreement / Disagreement

Participants express differing views on the compilation issues, with some focusing on file naming conventions while others emphasize the command line compilation process. The discussion remains unresolved regarding the specific cause of the compilation failure.

Contextual Notes

There are missing assumptions about the project structure and the exact command being used to compile the files. The discussion does not clarify the relationship between the provided code snippets and the reported error.

MarcL
Messages
170
Reaction score
2
Hey so I have to compile a project... there are two parts to it, one where I just have to display a series of sentence and another one a bit more complicated... I am using tutorialspoint.com because I don't have any compiler on the desktop at school :/ It is unfortunately giving me this error:

"sh-4.2# javac HelloWorld.java
javac: file not found: HelloWorld.java
Usage: javac <options> <source files>
use -help for a list of possible options"

However, when I compiled my first program ( the one that I'll post afterwards,) it compiled fine on its own. Any help? ( I started java 2 weeks ago, so many stupid errors might be included, sorry !)

Code:
import java.util.Scanner;

 public class SongsAndApps{

  public static void main(String []args){
  
  Scanner prePay = new Scanner(System.in);
  
   int songBuy , appBuy1, appBuy, songBuy1 ;
  
   Double moneyOnCard, moneyLeft, moneyLeft2, moneyLeft3, moneyLeft4;
  
   final int appCost=3 , songCost= 7;
  
  System.out.println("Please input the amount of money on your card");
  moneyOnCard = prePay.nextDouble();
                   //Determine how much there is on the card from the user
  songBuy =  (int)(moneyOnCard / songCost) ;
                 //Determines how much song the user is able to buy
  appBuy  = (int)(moneyOnCard / appCost) ;
                 //Opposite of last equation, allows to know how much apps can be bought
  System.out.println("You can buy a maximum of " + songBuy + " songs or you can buy a maximum of " + appBuy + " apps");
  moneyLeft = moneyOnCard - (songBuy*songCost) ;
  moneyLeft2 = moneyOnCard - (appBuy*appCost) ;
                   // moneyLeft represent the money left on the card if songs are bought first and moneyLeft2 represents the amount left if apps are bought
 
   if (moneyLeft >= 3 );                     
  {  appBuy1 = (int)(moneyLeft / appCost);
  moneyLeft3 = moneyLeft - (appCost*appBuy1);
  System.out.println("You can buy " + appBuy1 + " app if you decide to buy the maximum amount of songs");
  System.out.println("Therefore, You have " + moneyLeft3 + " $ left on your card") ;}
  if (moneyLeft2 >=7);
  { songBuy1 = (int)(moneyLeft3 / songCost);
  moneyLeft4 = moneyLeft3 - (songCost*songBuy1);
  System.out.println("Otherwise. you can buy " + songBuy1 + " song(s) if you decide to buy the maximum amount of apps");
  System.out.println("You have " + moneyLeft4 + " $ left on your card") ; }
  }  
}

That worked fine when I had it in a project folder alone. However, when I added a new class to the project( the following one, it stopped working)

Code:
 class MyFirstProgram.Java {
   
  public static void main(String[] args) {
   
  System.out.println("Welcome to My First Java Program");
  System.out.println("This Program was written by: Marc-Andre Leclair");
  System.out.println("The assignment was done individually on January 30th, 2015 at 19:34");
  System.out.println("End of program");
  System.exit(0)   
  }
 }

However when I did something like that on eclipse it ran and compiled fine :/ any help?
 
Technology news on Phys.org
For Java the class name must match the filename so for your non working example the filename should be SongsAndApps.java
 
It is that name which is odd. I know that the class containing the main method has to bear the same name as the file name. That's why I am completely confused
 
It's a bit difficult to understand where you're having compile problems. At the beginning of your first post, you're trying to compile a HelloWorld program which is not in either of the code examples. However, from the code examples, it looks like you're trying to compile several programs that have a main() method at once using the command line. Usually an IDE like Eclipse will take care of that for you when you choose to compile a 'package' (as opposed to a single class). If that's what you're trying to do with the command line, then you would need to do something like this:

javac -classpath . SongsAndApps.java MyFirstProgram.java

Source: Easy command line Java compile
 

Similar threads

Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 2 ·
Replies
2
Views
2K