I have a simple file system, in which all the files being used by the main program are in the same folder. The main program is also in the same system. It was actually a jar file by my friend and then I unzipped it using winrar, the class files are running well, but only when i amended something and tried to compile the program i am getting this error of cannot find symbol for one of the class being used, and its right there in the same folder. How can i resolve this please?
Thanks in advance
You have to place your source files into appropriate subfolders. The name of the folder corresponds to your package name, in this case next.
next/Pretty.java
next/Tree.java
...
Then you can compile using command
javac next/*.java
from the base directory.
Related
I am a Java newbie, just started learning it in college, and my class is using NetBeans but I'd like to use VSCode.
The professor told us that every Java file should start with:
package nameofthepackage
So that Java knows to which package the class (file) I created belongs to.
So I always create this structure:
I create a folder with the name of the main class, and inside this folder I make a src folder that will store the Java files. Eg:
MyJavaProject/src/MyJavaProject.java
I always name the main .Java file the same as the project folder name.
And when I compile I use javac with the -cp parameter to specify that the src folder is the classpath folder, where it should look for the .Java files I create.
I also always tell javac to compile all the files inside the src folder, using the * wildcard.
The issue is that with this line on top of my .Java files, javac compiles all the files, but I can't execute the bytecode because it complains it can't find the classes I created, even the main one.
As Soon as I remove the package line from the top of the files, I can compile and run the code.
So far it's good, but for any more complex projects this is gonna be really annoying.
Any ideas how can I fix this?
You need to specify the full name (with package) of the class that contains the main function.
Assuming the class MyJavaProject is the one containing your main, that would be:
java nameofthepackage.MyJavaProject
This is assuming you built it with
javac -d . MyJavaProject.java
It would create your target class in a directory structure like:
nameofthepackage\MyJavaProject.class
I am new to Java and JNA. I was trying to access the native interfaces of a third party applications using JNA from Java eclipse. So I was trying to set the jna.library.path from Command line using the command
java -Djna.library.path=<path to your library> MainClass
I have given the "path to your library" as the path of the folder where my .dll file and its dependencies are there. for ex: D:Adrin\Library\Dll.
"MainClass" I have given the name of the class with main() in my application(Jnaexe.java). But an error occurs saying
Error: Could not find or load main class Jnaexe.java
I have also tried by giving the package name and also the whole path of the Jnaexe.java. Still it does't work. I tried running from the src folder and even the folder where Jnaexe.java file is there(example folder) in command prompt. But the error was same.What I am missing here. Any help will be highly appreciated. Thank you in advance
I have been editing, running, and compiling code in Notepad++ using NppExec. I have set a classpath at C:\Java. This folder contains: C:\Java\com\DOMAINNAMEWITHHELD\Classes. Inside this folder I have 3 .java files, and one .class file (Runner.java, Pirate.java, Ninja.java, and Pirate.class). I was running these files in a folder on a flash drive, and none of the three could see each other. So I moved them to my new classpath defined directory, and still cannot get anything except:
C:\Java\com\DOMAINNAMEWITHHELD\Classes\Runner.java:12: error: cannot find symbol
phil.throwAStar(tim);
throwAStar() is defined in Ninja, and called in Runner.
In the three source files, I defined a package like this:
package com.DOMAINNAMEWITHHELD.Classes;
If I am in any other directory, and try:
import com.DOMAINNAMEWITHHELD.Classes.*;
I get the exact same error. What am I doing wrong?
So Once I cleaned up some syntax errors from troubleshooting, and trying to keep up with class, I finally got Runner, Ninja, Pirate, and now Wizard to compile, and run. In case you guys are wondering how to run a .class file contained in a package:
Let's say the file is: C:\Java\com\DOMAINWITHHELD\classes\Runner.class
Type this into a command prompt to run Runner:
cd C:\Java
java com.DOMAINWITHHELD.classes.Runner
Now the class 'Runner' in the package 'com.DOMAINWITHHELD.classes' should execute.
I have made a small program in Java that displays its .java source with a gui. It does not use FileChooser to do this. I am reading the .java sources with the aid of following statements
String resName = "/dev/classes/"+name+".java"
Scanner s = new Scanner(FilePrinter.class.getResourceAsStream(resName));
where name is the name of the .java file i.e. if the file is MyProg.java then name==Myprog. Of course my program is inside the dev.classes package
The thing is that when I export my project to JAR and include source files this works because source files reside inside the /dev/classes/ directory.
However, I haven't yet discovered a way to make my program run in Eclipse or from the command line without giving me exception.
And of course when someone tries to add those source files to be used automatically as resource files the process fails.
Can I somehow use the same code both when running from Eclipse and from the JAR? It must be something very trivial but because I am not Java expert I cannot see.
I found how to do it. Actually you either have to use Ant or Maven. However, this can be done in Eclipse as well as follows:
On the Eclipse Project Properties>Java Build Path you can choose on the bottom Default Output folder: <your_project_name>/src.
This causes class files be compiled in the same directory as the .java files and finally does what I wanted.
Thanks to #AndrewThompson for suggesting to try this
With reference to this post
Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line
I did not understand how to solve the problem
Actually in my java source code there' s line :
package es_2011;
when I compile the program through JCreator everything works perfectly.
It creates a folder named es_2011 where to put .class files.
Also the executing operation goes smoothly, the program runs ok.
Now I'd like to use the command line only.
So I placed my java file in the directory where javac.exe is but whenever I try to compile I get the same error
The command I use is: javac ProgAudioJ.java
The path (where javac.exe is ) is : C:\Program files\Java\jdk1.6.0_22\bin
Is someone willing to help me understand in terms of exactly tell me what I have to do?
thanks very much...MAX
The setup used for the looks like this (under windows)
C:\classDir -> is the project
C:\classDir\testpackage -> is the only package used (the package "testpackage")
C:\classDir\testpackage\Main.class -> is the class with the main method inside (important: it is the .class and not .java)
The Main.class looks like following:
package testpackage;
public class Main {
public static void main(String[] args) {
System.out.println("Program started! ;-)");
}
}
go with your command prompt to:
c:\classDir> java testpackage.Main
the result:
Program started! ;-)
According to your problems that it starts in your IDE but not from the console:
- checked if you realy use the path to the .class files?
- with the console go to the directory of you .class files, not the project (e.g. in Eclipse it is the bin directory
- enter the full qualified class name (including packages seperated by . -> e.g. testpackage.Main
More infos can be found under:
http://www.oracle.com/technetwork/java/compile-136656.html
Hope it helped
MAX, if the class defines that it's inside the package es_2011, then it should be in a folder with the same name.
So in your case, put the ProgAudioJ.java in the folder es_2011 and then run
javac es_2011\ProgAudioJ.java
latter to run it, you need the command
java es_2011.ProgAudioJ
You should add javac.exe in your path .Edit your path variable and append path to jdk's bin
then put java file in a dir named es_2011 , as the package declaration is es_2011 then compile
c:\es_2011\javac YourJava.java
and now go back to C:
c:\java es_2001.Yourjava
After reading you other Post: "Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line" I guess you go to the directory es_2011 where your ProgAudioJ.class file is located and run
java ProgAudioJ
from that folder.
instaend you have to go to the folder above (cd ..) and run
java es_2011.ProgAudioJ
Each package in Java corresponds to a folder on the filesystem. So a package declaration such as com.stackoverflow would mean that the source classes need to be in a folder ./com/stackoverflow. Typically the whole project would have a separate src folder containing com/stackoverflow.
When you compile the Java classes you DO NOT need to put source files in the same directory as javac.exe, you do however need to make sure that javac.exe is in your operating systems PATH variable. This tells the operating system where it should look for executable files when a command is run, on a *nix machine this would usually be /usr/bin or just /bin but on Windows machine the executables normally live within the applications own directories, that is C:\Program Files\something. Assuming that you've installed JDK correctly, the javac.exe should already be in the PATH you can check this by opening the command line and just running javac (just like that). If you get some output then all is well, the system knows where to find javac.exe.
Next you will need to go to your project folder and type javac -d . src/com/stackoverflow/MainSO.java notice that is run from the project folder. This will create a folder called com in your project root and put the compiled classes in com/stackoverflow. The -d flag tells javac where to put the compiled classes, if you leave that out, the compiled classes will be where the sources are.
Then when you want to run the classes you type java com.stackoverflow.MainSO (no .class). Crucially this command will need to be ran in the directory that contains the root of the class hierarchy (that is the com folder containing the compiled classes). You can specify other places for java to look for the classes by providing a classpath to the java command with the -cp flag. By default the classpath will contain the directory the java command was ran in. Should your project have dependencies external .jar files for example you will need to provide every single one of them (with their full filepath) in the classpath (this goes for the compiler as well). The IDEs do this automatically for you.
Hope that helps.