I am compiling a java source file named MyPeogram.java. Now I want to use javac to compile the file and save the class file in the name MyCompileCode.class. Is it possible to give such custom class names to compiled files?
Please let me know how to do this. I searched online a lot but could not find anything.
No. The name of the source file must match the name of the compiled file.
AFAIK, I think it is not possible, because Java is compiled and interpreted both. Even though you compiled the program and changed, it's name, the interpreter gets confused in class file name and the data in that class file.
Correct me, If I am wrong.
Related
I had been creating an online judge platform like codeforces.com on Django recently. Now, I'm struck across a problem that the user may possibly upload a Java file for testing with the filename different from the public class defined inside it. Yet, I should compile it automatically. So, what could be a possible solution to this?
If it's impossible to compile the file without renaming it properly, I need guidance how this 'renaming' can be done using a script/code?
It will be a compilation error. file name should be same as public class name.
hence not possible at all
The easiert would be probably to parse the uploaded Java file using something like JavaParser, then extract the package name and top-level class name and rename the uploaded file accordingly.
By the way, make sure you run the uploaded code in a pretty sandboxed environment.
I have been googling around, trying to understand what the Java Classpath and Path are. However, I am stil not quite sure if I have understood it or not. If feel that this topic is one of those grey areas.
Can someone explain me what those are? I mean, where do I find and set them (where is the actual text file on Mac/Windows)? Is there only one instance of each one? If so, how do I set the path for multiple classes?
As you might have notices, I am totally confused right now after reading so many different tutorials... So now I really would like to have a straight forward explanation.
Please help me, I just trying to learn :)
Thank you all
A path is just a folder location. The path is where your OS will look for programs by default. If java, javac, javap, etc, etc, are in your path then you can just type their names without the entire folder location.
Your classpath is similar. It is a set of folders that contain .class files describing classes(hence the name) and .jar files, which are basically files that contain .class files. All code that you're running is either out of the classpath, generated, or out of the java libaries(also part of the classpath, techncically).
With each run of a java program you can specify a classpath by parameters passed to the java executable. It also grabs classes out of "extension folders,", special folders Java keeps around to act as a system classpath, and finally, the "bootstrap classes", which are a set of important classes almost any Java program needs to run.
Simple mean of path is location of file system. if you want to access any file then you have to manually needs to go there location.
just example: d:\text1.txt then needs to go that d:\ location. same way java program have command like
javac -for compile
java - for run
.
.
.
etc.
that inside java-jdk\bin folder
so if you don't set into classpath. then you can execute java program like
run->cmd
c:\jdk1.6\bin> javac test.java
so without going explicit way you can set it into classpath, and direct execute java program from anywhere.
You can set java path as environment variable of computer.
The PATH is basically where your JDK is installed; this is essentially what your IDE will look for when trying to compile or create Javadoc or such; it's basically just the location of a folder on your hard drive, set as a Windows (or other OS) environment variable to make it easier to use.
The CLASSPATH is a property that tells the compiler where to look for classes. Basically if you download a library or such from somewhere, you need to add it to the CLASSPATH for the compiler to use it. Usually you can do this in your IDE, however, you should not need to directly access the CLASSPATH variable.
By the way, the Wikipedia article is pretty helpful.
1)java Path: it is location of binary executable files
example :javac , java
this file are used for compile and run
2)class Path: it is location of .class file(file create after compile your source code .java file)
I am using Notepad++ to write my Java code and Command Prompt to compile and run it.
Following is my sample Java code,
package abraKadabra;
public class SuperClass{
protected int anInstance;
public static void main(String [] abc){
System.out.println("Hello");
}
}
However, this file is in the following folder structure :
"usingprotected\superPkg" (usingProtected is a folder somewhere in the hierarchy in C:)
So, my package name here should be something like usingProtected.superPkg instead of abraKadabra as I wrote it.
But, when I compile this Java code from command prompt, it compiles fine with no error or warnings. Why is it so? Shouldn't the package name adhere to the folder structure?
And if it should, how would it adhere?
For e.g. if my package name is usingProtected.superPkg, will the compiler check in the reverse order. The present working directory should be superPkg, then the parent directory should be usingProtected and its done. Is it how it checks the folder structure with package name?
The Java language specification doesn't force files to be in a certain directory. It optionally allows the compiler to require that public classes are in files with the same name of the class, but I don't think there's anything similar for packages. Section 7.2.1 talks about possible storage options in a file system, but it doesn't say anything about enforcing source code structure, as far as I can see.
However, it's best practice - and a pretty much universally accepted convention - to reflect the package structure in the source directory structure... and javac will use this to try to find source files which aren't explicitly specified to be compiled.
Note that if you're compiling from the command line, by default each class will appear in the same location as the corresponding source file, but if you use the "-d" option (e.g. "-d bin") the compiler will build an appropriate output directory structure for you, rooted in the specified directory.
After experimenting a bit, I got the way how to use package name and run Java class files from command prompt.
Suppose following is my Java source file:-
package mySample;
public abstract class Sample{
public static void main(String... a){
System.out.println("Hello ambiguity");
}
}
This file is in directory "D:\Code N Code\CommandLine".
Now, when compile the source code (by going to the above directory from cmd) using following command:-
javac -d . Sample.java
This automatically creates "mySample" folder in my current directory. So, my class file Sample.class is present in directory "D:\Code N Code\CommandLine\mySample". Compiler created this new folder "mySample" from the package name that I gave in my source code.
So if I had given my package name to be "package com.mySample", compiler would create two directories and place my class file in "D:\Code N Code\CommandLine\com\mySample".
Now, I am still in the present working directory i.e. in "D:\Code N Code\CommandLine". And to run my class file, I give the following command:
java mySample.Sample
So, I give the complete hierarchy of package and then the class name. The Java Interpreter will search the current directory for "mySample" directory and in that for "Sample.class". It gets it right and runs it successfully. :)
Now, when I asked that why it compiles my wrong package source code, it would compile the code successfully though, but it gives NoClassDefFoundError when I run my class file. So above method can be used to use package names from command line.
If you're compiling a single class, javac doesn't need to look elsewhere for it. It'll just compile the file as is and put the resulting .class into the same folder. However, you generally won't be able to use the class til you put it into an "abraKadabra" directory in one of the directories in the class path.
If your class uses another class in the package, though, you might have problems compiling it where it is, for the same reason (javac wants to find the class and make sure it has the methods and such that your class uses).
Java compiler does not check the directory structure when it compiles source files. As you mentioned, suppose you have a source file that starts with the directive
package abraKadabra;
You can compile the file even if it is not contained in a subdirectory .../abraKadabra . The source file will compile without errors if it doesn’t depend on other packages. However, the resulting program will not run (unless also including package name in execution). The virtual machine won’t find the resulting classes when you try to run the program.
I have a Java File # C:\Drive\MyFile.java
Now I want to read all the methods inside the java file.
I know about Class.forName() but how to pass local path inside the forName.
you would need to compile or at least parse the java file and look for methods. you will have better luck loading the compiled class file and using http://download.oracle.com/javase/6/docs/technotes/guides/reflection/index.html
There is nothing in java that can help with .java files, just .class file. Even then, you are not assured of successfully loading an arbitrary class file, since you will probably be missing a dependency.
If I had to do this, I would run javadoc with the -public, -package, -protected and -private on every .java file I found. This gives me an HTML file with a regular format that can easily be parsed. If I was more ambitious, I would write a custom javadoc doclet. If I was crazy ambitious, I would use antlr, yacc, javacc or another parser generator with a Java grammar to parse the Java files directly.
Try using reflection, which allows you to find variables, methods, constructors, etc. defined in a Java class.
I am facing this problem while trying to run my java file by writing java filename ....
I have read on many pages the possible ways this could be corrected but unfortunately I have been unable to correct my problem...
First of all I looked at my environment variables and observed that there was no CLASSPATH set and I had pointed PATH correctly to my jre as well as jdk bin in C:\
Second I am able to run javac filename.java and observe that .class file gets built in the local directory.
While writing javac -classpath . filename works writing java -classpath . filename (without .class) results in the same error.
I just don't know how to run my program in command prompt!!!!
Please do not give me links to the pages which have given the same answers that I have mentioned above as they do not work in my case.....
Please help....
Note that if your class resides in some package mypackage, you need to make sure the class file is inside mypackage/ and do
java -classpath . mypackage.YourClass
There is a bit little information in your post ... as said, a complete example would really help solving this. Some ideas:
Is class named filename? If not, you should make sure to call the right class name in the java command.
if you have strange characters in your class name (basically anything apart from A-Z, a-z, 0-9, _), it could be mangled by your file system and thus lead to java not being able to find it. Make sure that the class name is the same as the file name.
If you use packages, make sure the package names are congruent to your file structure (see latest questions with tag package for some examples).
Is your class public? If not, make it so. (This should give another error, though.)
Stating javac filename.java and java filename are not enough for this really.
We also need to know the contents of the filename.java or at least the first few lines.
This is the expected way to declare a class
public class Filename {
}
This is the way it would have to be to work given your example: java filename
public class filename {
}
This is could exist too, but you are probably just messing with us :)
public class SomethingElse {
}
Overall no matter what the filename is on the filesystem, the class has a name and that is the name that the java command is expecting. I would recommend using the upper case first letter form as it is clearer imo