This question already has an answer here:
How to bypass permission denied error?
(1 answer)
Closed 5 years ago.
I just begin to learn Java, and write a simple java code to define class
import java.util.*;
class People {
People() {
System.out.println("New people");
}
}
public class Test {
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
new People();
}
}
}
Then I use javac to compile it, and some error happened
Test.java:3: error: error while writing People: People.class (Permission denied)
class People {
^
1 error
How can I solve it?
error while writing People: People.class (Permission denied)
It has no relation with java code.
You execute the javac command with a user that has not the rights to create a file (the generated .class) where you try to do it.
Just check the rights of the user and the folder where you generate the .class files and adjust the one or the other.
Related
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 8 months ago.
I try to make a game Flappy Bird and want to test my result on DesktopLauncher, but something is happening that makes two errors. I can't find any F on the list.
package com.mygdx.flappy;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
public class DesktopLauncher {
public static void main (String[] arg) {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.width = 480; //this is the error part
config.heigth = 800; //this is the error part
new Lwjgl3Application(new MainFlappyBird(), config);
}
}
here's the picture
Config is an object of type „Lwjgl3ApplicationConfiguration“ And this type does not have properties „width“ and „height“. That‘s why you can‘t assign values to this property, because it does not exist.
This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 1 year ago.
I am following along with JAVA book for CS101 and trying to wrap my head around this exercise, but when I run my code, I keep getting this error
Error: Could not find or load main class Riddle
Caused by: java.lang.ClassNotFoundException: Riddle
JAVA CODE
public class Riddle{
private String question; //instance variables
private String answer;
public Riddle( String q, String a) //constructor
{
question = q;
answer = a;
}
public String getQuestion() //instance method
{
return question;
}
public String getAnswer()
{
return answer;
}
}
public class RiddleUser{
public static void main ( String argv [] ){
Riddle riddle1= new Riddle (
"What is black and white and red all over?",
"An embarrassed zebra.");
Riddle riddle2= new Riddle (
"What is black and white and read all over?",
"A newspaper." );
System.out.println("Here are two riddles:");
System.out.println(riddle1.getQuestion());
System.out.println(riddle2.getQuestion());
System.out.println("The answer to the first riddle is:");
System.out.println(riddle1.getAnswer());
System.out.println("The answer to the second riddle is:");
System.out.println(riddle2.getAnswer());
}
}
Here is the exercise I am doing, I also uploaded the PDF of the book, the exercise is on pages 71-73.
Basically, it wants to show you how to write a class and test it, If I am getting this correctly.
enter image description here
enter image description here
enter link description here
On a Java file, only 1 class can be public, the public class is the one with the main method.
The file and the public class must have the same name, RiddleUser.java in this case.
After declaring RiddleUser class in its own file.
This should work if you add private scope modifier to the RiddleUser class, without declaring it in its own file aswell.
Say your source path folder is ~/riddle-project/src/
riddle-project
└── src
├── Riddle.java
└── RiddleUser.java
Execute the following:
# compile main class
$ javac src/Riddle.java
# run main class with specified class path
$ java -classpath ./src/ Riddle
Class Path is the directory that contains the .class compiled java files
I'm coding a Library program for my classroom, but I ran into an error quite immediately after compiling. The challenge for this program is that I need to use 3 files, only one of which (libraryRunner) actually has a Main in it. I'm trying to get both of the other method files (libraryCard and libraryBooks respectively) to work together.
I used Booleans before, and switched to Ints to see if that would fix it, but i get the same error. What's weird is that the program seems to compile, but the program just exits after the previously mentioned error.
Library Runner :
import java.util.Scanner;
public class libraryRunner
{
public static void main (String args[])
{
libraryCard Joseph = new libraryCard();
Joseph.whichBook("Sing");
}
}
What i'm using in Library Card :
...
public static String whichBook(String c)
{
if(libraryBooks.c == 1)
{
return("Sorry, it's already been checked out.");
}
else
{
return("Here's your book, " + c + "!");
}
}
}
What i'm using in Library Books:
public class libraryBooks
{
private static int hasBeenCheckedOut;
public libraryBooks()
{
libraryBooks.Sing(0);
}
public static int Sing(int c)
{
hasBeenCheckedOut = c;
return(hasBeenCheckedOut);
}
...
From what even my teacher tells me, the program should in theory compile and run, telling the user that the book "Sing" is now theirs. But I only get this error :
----jGRASP exec: java libraryRunner
Exception in thread "main" java.lang.NoSuchMethodError: libraryCard.whichBook(Ljava/lang/String;)Ljava/lang/String;
at libraryRunner.main(libraryRunner.java:8)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
On-the-fly, in-memory java code compilation for Java 5 and Java 6
Compiling Java file with code from within a Java file
i have a hello world class available in the string of a program as given in the example below,
public class CompileJavaString {
public static void main(String arg[]) {
String s="public class HelloWorld{ public static void main(String arg[]) ";
s=s+" { System.out.println(\"Hello World\"); } } ";
// this is the complete code of Hello World class taken as an example
// code to compile the class Hello World available in string and
// generate the HelloWorld.class file required here
}
}
can someone help to compile the code in a memory string available in example given above
You want to have a look at javax.tools.JavaCompiler and related classes.
The documentation contains examples of how to use them.
Note that the java compiler will only work if you have a JDK installed. A JRE is not enough.
Save as HelloWorld.java and do following:
String fileToCompile = "HelloWorld.java";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, null, null, fileToCompile);
if(compilationResult == 0){
System.out.println("Compilation is successful");
}else{
System.out.println("Compilation Failed");
}
Edit
Can have a look at detailed example :
http://www.java2s.com/Code/Java/JDK-6/CompilingfromMemory.htm
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Copying files from one directory to another in Java
I have a folder (c:/InstallationFiles) with .jar files in it. i want to read through it and if the name equals test1 i want to copy it to a test1 folder, then if the name is test2 copy it to a test2 folder etc. this is what i have so far:
private static int copyJARFiles() {
resultCode = 0;
File installFolder = new File(Constants.WINDOWS + Constants.INSTALLATION_FOLDER);
File[] installFiles = installFolder.listFiles();
for (int i = 0; i < installFiles.length; i++) {
if (installFiles[i].equals("test1.jar")){
}
if (installFiles[i].equals("test2.jar")){
}
}
return resultCode;
}
not sure how to copy it then. im still a rookie.
thank you / kind regards
if you want to copy jar:
You can use apache IO api. Use the below code:
FileUtils.copy(sourceFile,destinationFile);
You can also use java 7. It contains direct function to copy files.
if you want extract jar:
You can use java.util.zip.*; package classes.
Please let me know if you need more explanation.
Not sure I fully understood your task but maybe this example will help you
for (File f : installFolder.listFiles()) {
if (f.getName().endsWith(".jar")) {
File targetDir = new File(installFolder, f.getName().replace(".jar", ""));
if (!targetDir.exists()) {
targetDir.mkdir();
}
File target = new File(targetDir, f.getName());
Files.copy(f.toPath(), target.toPath());
}
}
The main idea is that Java 7 provides us with Files.copy util