[Java] Hilfe beim compilieren

Dieses Thema im Forum "Programmierung & Entwicklung" wurde erstellt von cheffeof, 19. Mai 2007 .

Status des Themas:
Es sind keine weiteren Antworten möglich.
  1. 19. Mai 2007
    Hilfe beim compilieren

    Hi.
    Also folgendes:
    Habe mir das JDK 1.6 von Oracle and Sun Microsystems | Strategic Acquisitions | Oracle runtergeladen. Danach habe ich ein kleines Applet geschrieben.
    Und wollte es compilieren. Wo sich der compilier (javac) befindet ist mir auch bekannt.

    Mein Problem nun:
    Ich kriegst über ich Console nicht compiliert. (jacac Applet1.java) hat nicht funtioniert.
    Dann habe ich den pfad zur javac angegeben, klappte auch nicht.
    Der Quellcode ist fehlerfrei und habe es auch unter dem Namen der Klasse gespeichert.
    Aber nun gehen mir die Ideen aus.

    Deshalb bitte ich um eure hilfe und gute Lösungsvorschläge.

    mfg cheffe
     
  2. 19. Mai 2007
    AW: Hilfe beim compilieren

    Wie lautet denn die Fehlermeldung?
     
  3. 19. Mai 2007
    AW: Hilfe beim compilieren

    Also es kommt das er den Befehl javac nicht kennt.
     
  4. 19. Mai 2007
    AW: Hilfe beim compilieren

    editier mal die Path variable(google) auf dein Javasdk bin ordern. Z.B "C:\Programme\Java\jdk1.6.0_01\bin". Danach sollte es eingentlich funktionieren
     
  5. 19. Mai 2007
    AW: Hilfe beim compilieren

    das hab ich doch auch schon gemacht, hat auch nicht funtioniert.
     
  6. 21. Mai 2007
    AW: Hilfe beim compilieren

    Mit was hast du den das Applet geschrieben, Texteditor? Oder wieso willst du es mit der win console complilieren? Brauch doch einfach den Compiller von z.B. Eclipse...

    piis
     
  7. 22. Mai 2007
    AW: Hilfe beim compilieren

    Ja, ich habs mit nem Editor geschrieben. Ich würds ja auch gerne über nen java editor kompilieren, aber das ist leider nicht die aufgabe.
    mfg cheffe
     
  8. 22. Mai 2007
    AW: Hilfe beim compilieren

    Dass der javac nicht kennt, heißt, dass du immernoch noch nicht weiß wo die javac wirklich liegt! Denn wenn man den richtigen Pfad benützt, findet der auch die javac ergo -> der Befehl javac ist bekannt!
     
  9. 23. Mai 2007
    AW: Hilfe beim compilieren

    Dann geh doch mal zum test in das java sdk / bin verzeichniss und versuche von dort dein progg zu compilen...
     
  10. 23. Mai 2007
    AW: Hilfe beim compilieren

    Was auch gut sein kann, dass du die Umgebungsvariable einfach falsch gesetzt hast. Also nicht den Pfad, aber vielleicht ein Leerzeichen oder ein falsches Trennzeichen. Da war Windows bei mir auch sehr empfindlich...

    MfG
    Bernie
     
  11. 25. Mai 2007
    AW: Hilfe beim compilieren

    Hi

    so könntest du das machen, ist ein beispiel:

    Code:
    package de.tutorials;
     
     import java.io.BufferedOutputStream;
     import java.io.BufferedReader;
     import java.io.ByteArrayOutputStream;
     import java.io.FileInputStream;
     import java.io.FileOutputStream;
     import java.io.FileWriter;
     import java.io.IOException;
     import java.io.InputStream;
     import java.io.InputStreamReader;
     import java.io.Reader;
     import java.util.ArrayList;
     import java.util.HashMap;
     import java.util.List;
     import java.util.Locale;
     import java.util.Map;
     import java.util.StringTokenizer;
     
     import org.eclipse.jdt.core.compiler.IProblem;
     import org.eclipse.jdt.internal.compiler.ClassFile;
     import org.eclipse.jdt.internal.compiler.CompilationResult;
     import org.eclipse.jdt.internal.compiler.Compiler;
     import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
     import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
     import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
     import org.eclipse.jdt.internal.compiler.IProblemFactory;
     import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
     import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
     import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
     import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
     import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
     import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
     
     public class JDTCompilerExample {
     
     /**
     * @param args
     */
     public static void main(String[] args) {
     
     String compilationUnitContent = "package de.tutorials;\n"
     + "public class Fooooooo{\n" + "public static void burp(){\n"
     + "System.out.println(\"burrrrrrrrrrrrrrp\");" + "}\n" + "}";
     
     final String sourceFile = "E:/eclipse/3.1/eclipse/workspace/de.tutorials/src/Fooooooo.java";
     
     try {
     FileWriter fw = new FileWriter(sourceFile);
     fw.write(compilationUnitContent);
     fw.flush();
     fw.close();
     } catch (IOException e1) {
     e1.printStackTrace();
     }
     
     final String outputDir = "E:/eclipse/3.1/eclipse/workspace/de.tutorials/bin";
     final String packageName = "de.tutorials";
     final String targetClassName = "de.tutorials.Fooooooo";
     final ClassLoader classLoader = JDTCompilerExample.class
     .getClassLoader();
     
     final String fileNames[] = { sourceFile };
     final String classNames[] = { targetClassName };
     
     final List problemList = new ArrayList();
     
     class CompilationUnit implements ICompilationUnit {
     
     String className;
     
     String sourceFile;
     
     CompilationUnit(String sourceFile, String className) {
     this.className = className;
     this.sourceFile = sourceFile;
     }
     
     public char[] getFileName() {
     return className.toCharArray();
     }
     
     public char[] getContents() {
     char[] result = null;
     try {
     InputStreamReader isReader = new InputStreamReader(
     new FileInputStream(sourceFile));
     Reader reader = new BufferedReader(isReader);
     if (reader != null) {
     char[] chars = new char[8192];
     StringBuffer buf = new StringBuffer();
     int count;
     while ((count = reader.read(chars, 0, chars.length)) > 0) {
     buf.append(chars, 0, count);
     }
     result = new char[buf.length()];
     buf.getChars(0, result.length, result, 0);
     }
     } catch (IOException e) {
     e.printStackTrace();
     }
     return result;
     }
     
     public char[] getMainTypeName() {
     int dot = className.lastIndexOf('.');
     if (dot > 0) {
     return className.substring(dot + 1).toCharArray();
     }
     return className.toCharArray();
     }
     
     public char[][] getPackageName() {
     StringTokenizer izer = new StringTokenizer(className, ".");
     char[][] result = new char[izer.countTokens() - 1][];
     for (int i = 0; i < result.length; i++) {
     String tok = izer.nextToken();
     result[i] = tok.toCharArray();
     }
     return result;
     }
     }
     
     final INameEnvironment env = new INameEnvironment() {
     
     public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
     String result = "";
     String sep = "";
     for (int i = 0; i < compoundTypeName.length; i++) {
     result += sep;
     result += new String(compoundTypeName[i]);
     sep = ".";
     }
     return findType(result);
     }
     
     public NameEnvironmentAnswer findType(char[] typeName,
     char[][] packageName) {
     String result = "";
     String sep = "";
     for (int i = 0; i < packageName.length; i++) {
     result += sep;
     result += new String(packageName[i]);
     sep = ".";
     }
     result += sep;
     result += new String(typeName);
     return findType(result);
     }
     
     private NameEnvironmentAnswer findType(String className) {
     
     InputStream is = null;
     try {
     if (className.equals(targetClassName)) {
     ICompilationUnit compilationUnit = new CompilationUnit(
     sourceFile, className);
     return new NameEnvironmentAnswer(compilationUnit);
     }
     String resourceName = className.replace('.', '/')
     + ".class";
     is = classLoader.getResourceAsStream(resourceName);
     if (is != null) {
     byte[] classBytes;
     byte[] buf = new byte[8192];
     ByteArrayOutputStream baos = new ByteArrayOutputStream(
     buf.length);
     int count;
     while ((count = is.read(buf, 0, buf.length)) > 0) {
     baos.write(buf, 0, count);
     }
     baos.flush();
     classBytes = baos.toByteArray();
     char[] fileName = className.toCharArray();
     ClassFileReader classFileReader = new ClassFileReader(
     classBytes, fileName, true);
     return new NameEnvironmentAnswer(classFileReader);
     }
     } catch (IOException e) {
     e.printStackTrace();
     } catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException e) {
     e.printStackTrace();
     } finally {
     if (is != null) {
     try {
     is.close();
     } catch (IOException exc) {
     // Ignore
     }
     }
     }
     return null;
     }
     
     private boolean isPackage(String result) {
     if (result.equals(targetClassName)) {
     return false;
     }
     String resourceName = result.replace('.', '/') + ".class";
     InputStream is = classLoader.getResourceAsStream(resourceName);
     return is == null;
     }
     
     public boolean isPackage(char[][] parentPackageName,
     char[] packageName) {
     String result = "";
     String sep = "";
     if (parentPackageName != null) {
     for (int i = 0; i < parentPackageName.length; i++) {
     result += sep;
     String str = new String(parentPackageName[i]);
     result += str;
     sep = ".";
     }
     }
     String str = new String(packageName);
     if (Character.isUpperCase(str.charAt(0))) {
     if (!isPackage(result)) {
     return false;
     }
     }
     result += sep;
     result += str;
     return isPackage(result);
     }
     
     public void cleanup() {
     }
     };
     
     final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies
     .proceedWithAllProblems();
     
     final Map settings = new HashMap();
     settings.put(CompilerOptions.OPTION_LineNumberAttribute,
     CompilerOptions.GENERATE);
     settings.put(CompilerOptions.OPTION_SourceFileAttribute,
     CompilerOptions.GENERATE);
     settings.put(CompilerOptions.OPTION_ReportDeprecation,
     CompilerOptions.IGNORE);
     settings.put(CompilerOptions.OPTION_LocalVariableAttribute,
     CompilerOptions.GENERATE);
     settings
     .put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4);
     
     settings.put(CompilerOptions.OPTION_TargetPlatform,
     CompilerOptions.VERSION_1_4);
     
     final IProblemFactory problemFactory = new DefaultProblemFactory(Locale
     .getDefault());
     
     final ICompilerRequestor requestor = new ICompilerRequestor() {
     public void acceptResult(CompilationResult result) {
     try {
     if (result.hasProblems()) {
     IProblem[] problems = result.getProblems();
     for (int i = 0; i < problems.length; i++) {
     IProblem problem = problems[i];
     if (problem.isError()) {
     String name = new String(problems[i]
     .getOriginatingFileName());
     problemList.add(new StringBuffer("Problem in ")
     .append(name).append("\nMessage: ")
     .append(problem.getMessage()).append(
     "\nFound at: ").append(
     problem.getSourceLineNumber())
     .toString());
     
     }
     }
     }
     if (problemList.isEmpty()) {
     ClassFile[] classFiles = result.getClassFiles();
     for (int i = 0; i < classFiles.length; i++) {
     ClassFile classFile = classFiles[i];
     char[][] compoundName = classFile.getCompoundName();
     String className = "";
     String sep = "";
     for (int j = 0; j < compoundName.length; j++) {
     className += sep;
     className += new String(compoundName[j]);
     sep = ".";
     }
     byte[] bytes = classFile.getBytes();
     String outFile = outputDir + "/"
     + className.replace('.', '/') + ".class";
     FileOutputStream fout = new FileOutputStream(
     outFile);
     BufferedOutputStream bos = new BufferedOutputStream(
     fout);
     bos.write(bytes);
     bos.close();
     }
     }
     } catch (IOException e) {
     e.printStackTrace();
     }
     }
     };
     
     ICompilationUnit[] compilationUnits = new ICompilationUnit[classNames.length];
     for (int i = 0; i < compilationUnits.length; i++) {
     String className = classNames[i];
     compilationUnits[i] = new CompilationUnit(fileNames[i], className);
     }
     Compiler compiler = new Compiler(env, policy, settings, requestor,
     problemFactory);
     compiler.compile(compilationUnits);
     
     //Hier probieren wir die gerade erzeugte Klasse einfach mal aus... ;-)
     try {
     Class.forName(targetClassName).getMethod("burp", new Class[0])
     .invoke(null, new Object[0]);
     } catch (Exception e) {
     e.printStackTrace();
     }
     
     }
     } 
    
    Greetz Snake
     
  12. 1. Juni 2007
    AW: Hilfe beim compilieren

    Auf jeden Fall ein großes Danke für eure Hilfe, mittlerweile funktionierts.
    mfg cheffe
     
  13. 4. November 2007
    Probleme beim Compilieren.

    Hi,

    Ich bin gerade dabei C++ zu erlernen und habe möchte das ganze Linuxbasierend machen und habe da auch schon ein TUT gefunden. Nur irgendwie klappts mit dem kompilieren noch nicht so. Wenn ich Hello World in emacs reinklop und das danach in der datei "bla" speichere und das dann nach bla.cpp compilieren will. Wie stell ich das genau an. Denn ich bekomme entweder eine "no input" oder "execvp not found" raus.

    Danke
    Mr_Meya
     
  14. 4. November 2007
    AW: Probleme beim Compilieren.

    Code:
    g++ bla.cpp
    erzeugt dir eine ausgabedatei namens "a.out" im aktuellen Verzeichnis. Wenn du die Datei noch selbst benennen willst, dann machs so:

    Code:
    g++ -o meinprog.out bla.cpp
    Gruß
    meckes
     
  15. 4. November 2007
    AW: Probleme beim Compilieren.

    Thx... hat einwandfrei funktioniert... der Rest ist ein anderes Problem -.-
     
  16. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.